• unique values

    From George Bouras@21:1/5 to All on Tue Feb 23 16:25:59 2021
    I want to iterate the unique values of a hash, without poluting the code
    with extra hash definition. Any better idea than

    my %hash = ( k1=>'v1', k2=>'v1', k3=>'v2', k4=>'v2' );

    foreach ( sub{local $_={}; @{$_}{@_}=1; keys %{$_}}->(values %hash) ) {
    say $_;
    }

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Randal L. Schwartz@21:1/5 to All on Tue Feb 23 07:27:52 2021
    "George" == George Bouras <foo@example.com> writes:

    George> I want to iterate the unique values of a hash, without poluting
    George> the code with extra hash definition. Any better idea than

    George> my %hash = ( k1=>'v1', k2=>'v1', k3=>'v2', k4=>'v2' );

    George> foreach ( sub{local $_={}; @{$_}{@_}=1; keys %{$_}}->(values
    George> %hash) ) { say $_; }

    Yes. Anything *but* that. You're not golfing here.

    my %hash = ( k1=>'v1', k2=>'v1', k3=>'v2', k4=>'v2' );
    {
    my %u;
    @u{values %hash} = ();
    print "$_\n" for sort keys %u;
    }

    And even *that* could use a comment or two for junior devs.

    print "Just another Perl hacker,"; # the original
    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 <merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/> Perl/Dart/Flutter consulting, Technical writing, Comedy, etc. etc.
    Still trying to think of something clever for the fourth line of this .sig

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Eric Pozharski@21:1/5 to George Bouras on Wed Feb 24 10:49:45 2021
    with <s1339k$rhi$1@gioia.aioe.org> George Bouras wrote:

    I want to iterate the unique values of a hash, without poluting the
    code with extra hash definition. Any better idea than

    my %hash = ( k1=>'v1', k2=>'v1', k3=>'v2', k4=>'v2' );

    foreach ( sub{local $_={}; @{$_}{@_}=1; keys %{$_}}->(values %hash) )
    { say $_; }

    By my books 'local $_={}' of yours is no different from 'my %foo'. IOW,
    it's not fulfilling requirements "without ... definition" -- you're not
    going 'local' for explicit effects of 'local' itself. Your code *is* foulfulling requirements, if I may.

    --
    Torvalds' goal for Linux is very simple: World Domination
    Stallman's goal for GNU is even simpler: Freedom

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From gamo@21:1/5 to All on Wed Feb 24 13:08:01 2021
    El 23/2/21 a las 16:27, Randal L. Schwartz escribió:
    "George" == George Bouras <foo@example.com> writes:

    George> I want to iterate the unique values of a hash, without poluting George> the code with extra hash definition. Any better idea than

    George> my %hash = ( k1=>'v1', k2=>'v1', k3=>'v2', k4=>'v2' );

    George> foreach ( sub{local $_={}; @{$_}{@_}=1; keys %{$_}}->(values
    George> %hash) ) { say $_; }

    Yes. Anything *but* that. You're not golfing here.

    my %hash = ( k1=>'v1', k2=>'v1', k3=>'v2', k4=>'v2' );
    {
    my %u;
    @u{values %hash} = ();
    print "$_\n" for sort keys %u;
    }


    Doesn't this do the same?

    my %hash = ( k1=>'v1', k2=>'v1', k3=>'v2', k4=>'v2' );
    my %v = reverse %hash;

    for (keys %v){
    print "$_\n";
    }









    And even *that* could use a comment or two for junior devs.

    print "Just another Perl hacker,"; # the original



    --
    http://gamo.sdf-eu.org/
    perl -E 'say "Press return to continue";'

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rainer Weikusat@21:1/5 to gamo on Wed Feb 24 15:03:33 2021
    gamo <gamo@telecable.es> writes:
    El 23/2/21 a las 16:27, Randal L. Schwartz escribió:
    "George" == George Bouras <foo@example.com> writes:

    George> I want to iterate the unique values of a hash, without poluting
    George> the code with extra hash definition. Any better idea than

    George> my %hash = ( k1=>'v1', k2=>'v1', k3=>'v2', k4=>'v2' );

    George> foreach ( sub{local $_={}; @{$_}{@_}=1; keys %{$_}}->(values
    George> %hash) ) { say $_; }

    Yes. Anything *but* that. You're not golfing here.

    my %hash = ( k1=>'v1', k2=>'v1', k3=>'v2', k4=>'v2' );
    {
    my %u;
    @u{values %hash} = ();
    print "$_\n" for sort keys %u;
    }


    Doesn't this do the same?

    my %hash = ( k1=>'v1', k2=>'v1', k3=>'v2', k4=>'v2' );
    my %v = reverse %hash;

    for (keys %v){
    print "$_\n";
    }

    It's mostly a more contorted, functional equivalent. The difference is
    that the output order of the loop is not defined.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From gamo@21:1/5 to All on Wed Feb 24 18:01:03 2021
    El 24/2/21 a las 16:03, Rainer Weikusat escribió:
    gamo <gamo@telecable.es> writes:
    El 23/2/21 a las 16:27, Randal L. Schwartz escribió:
    "George" == George Bouras <foo@example.com> writes:

    George> I want to iterate the unique values of a hash, without poluting
    George> the code with extra hash definition. Any better idea than

    George> my %hash = ( k1=>'v1', k2=>'v1', k3=>'v2', k4=>'v2' );

    George> foreach ( sub{local $_={}; @{$_}{@_}=1; keys %{$_}}->(values
    George> %hash) ) { say $_; }

    Yes. Anything *but* that. You're not golfing here.

    my %hash = ( k1=>'v1', k2=>'v1', k3=>'v2', k4=>'v2' );
    {
    my %u;
    @u{values %hash} = ();
    print "$_\n" for sort keys %u;
    }


    Doesn't this do the same?

    my %hash = ( k1=>'v1', k2=>'v1', k3=>'v2', k4=>'v2' );
    my %v = reverse %hash;

    for (keys %v){
    print "$_\n";
    }

    It's mostly a more contorted, functional equivalent. The difference is
    that the output order of the loop is not defined.


    You have only to add 'sort' before 'keys', but the OP did not.

    --
    http://gamo.sdf-eu.org/
    perl -E 'say "Press return to continue";'

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From George Bouras@21:1/5 to All on Wed Feb 24 20:29:41 2021
    Στις 24/2/2021 10:49 π.μ., ο/η Eric Pozharski έγραψε:
    with <s1339k$rhi$1@gioia.aioe.org> George Bouras wrote:

    I want to iterate the unique values of a hash, without poluting the
    code with extra hash definition. Any better idea than

    my %hash = ( k1=>'v1', k2=>'v1', k3=>'v2', k4=>'v2' );

    foreach ( sub{local $_={}; @{$_}{@_}=1; keys %{$_}}->(values %hash) )
    { say $_; }

    By my books 'local $_={}' of yours is no different from 'my %foo'. IOW,
    it's not fulfilling requirements "without ... definition" -- you're not
    going 'local' for explicit effects of 'local' itself. Your code *is* foulfulling requirements, if I may.



    indeed !

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)