Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: assigning arrays as values to keys of hash

by tybalt89 (Parson)
on Sep 18, 2018 at 20:06 UTC ( #1222606=note: print w/replies, xml ) Need Help??


in reply to assigning arrays as values to keys of hash

Why do it in two passes when it can be done in one pass? Efficiency is over-rated. (tybalt89 ducks :)

#!/usr/bin/perl # https://perlmonks.org/?node_id=1222551 use strict; use warnings; use Data::Dumper; my %hash; while( <DATA> ) { /(\S+)\s+(\S+)/ and $hash{$1} = [ keys %{{map {$_, 1} @{$hash{$1}}, +$2}} ]; } print Dumper \%hash; __DATA__ snake fangs snake tail snake fangs bird feathers bird beak snake scales bird beak bird claw bird wings

Replies are listed 'Best First'.
Re^2: assigning arrays as values to keys of hash
by Marshall (Abbot) on Sep 18, 2018 at 22:29 UTC
    Hi tybalt89!

    Great post!
    However, I am not convinced that your implementation would be more efficient than any of the "2 pass solutions".
    I thought my response to the OP at Re: assigning arrays as values to keys of hash to be reasonable and importantly: understandable by the OP.

    I think that sometimes PerlMonks fails new Perler's with overly complicated solutions that they can't understand or generalize.
    This OP is a beginner, not by user name, but by his original code.

    Your solution hides a foreach loop in terms of a map{} which does a lot of work. Shorter Perl code doesn't always mean "faster".

      It seems my "Efficiency is over-rated" comment was unclear. I fully believe (without testing, therefor as an article of faith) that my solution is slower than the two pass solutions. I guess I didn't make that clear. I am less interested in efficiency and more interested in solutions that show more rarely used perl capabilities.

      TIMTOWTDI forever :)

        Now that I understand what your objective was, all is fine.
        For a newbie, my objective is to show simple code that will "run like a rocket".
        The complications in your code are too much for a beginner.

        That sounds like a challenge :)

        #!/usr/bin/perl # https://perlmonks.org/?node_id=1222551 use strict; use warnings; use Data::Dumper; my %hash; $hash{ $& x /\S+/g }{ $& x //g } = 1 while <DATA>; $_ = [ keys %$_ ] for values %hash; print Dumper \%hash; __DATA__ snake fangs snake tail snake fangs bird feathers bird beak snake scales bird beak bird claw bird wings

        (I just wanted to post code that, if the problem is a homework assignment, will force the teacher to make the student go to the front of the class and explain on the blackboard, in detail, to the rest of the students, how the solution works :)

        What a hoot!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1222606]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2019-07-24 06:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If you were the first to set foot on the Moon, what would be your epigram?






    Results (32 votes). Check out past polls.

    Notices?