Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Working on hash slice

by catfish1116 (Sexton)
on Mar 12, 2019 at 18:23 UTC ( #1231174=perlquestion: print w/replies, xml ) Need Help??
catfish1116 has asked for the wisdom of the Perl Monks concerning the following question:

Working on small program that displays hash slice, Below is code and error.

## Program executes example of hash slice my @players = qw/ barney fred dino/; my @bowling_scores = (195, 205, 30); my @score { @players} = @bowling_scores; print "Tonight's players were @players\n"; print "Their scores were: @score{@players}"; syntax error at ./Page_280 line 11, near "@score { " Execution of ./Page_280 aborted due to compilation errors

TIA The Catfish

Replies are listed 'Best First'.
Re: Working on hash slice
by hippo (Canon) on Mar 12, 2019 at 18:34 UTC

    You cannot declare a hash as a slice like that. TIMTOWTDI but I would declare the hash first, then populate it:

    my @players = qw/ barney fred dino/; my @bowling_scores = (195, 205, 30); my %score; @score { @players} = @bowling_scores; print "Tonight's players were @players\n"; print "Their scores were: @score{@players}";
Re: Working on hash slice
by NetWallah (Canon) on Mar 12, 2019 at 18:48 UTC
    hippo (++) has the classic solution.

    If you want to declare AND populate, the following *DESTRUCTIVE* solution works:

    >perl -MData::Dumper -E "my @players = qw/ barney fred dino/; my @bowl +ing_scores = (195, 205, 30);my %score=map{shift @players,$_} @bowling +_scores;say Dumper \%score" $VAR1 = { 'dino' => 30, 'barney' => 195, 'fred' => 205 };
    For readability - the key statement is:
    my %score=map{shift @players,$_} @bowling_scores;
    Update: Here is a NON-Destructive version:
    >perl -MData::Dumper -E "my @players = qw/ barney fred dino/; my @bowl +ing_scores = (195, 205, 30);my %score=map{$players[$_],$bowling_score +s[$_]} 0..$#players;say Dumper \%score; say Dumper \@players"
    Key statement:
    my %score=map{$players[$_],$bowling_scores[$_]} 0..$#players;

                    As a computer, I find your faith in technology amusing.

Re: Working on hash slice
by AnomalousMonk (Chancellor) on Mar 12, 2019 at 23:34 UTC

    I agree that the method hippo shows here is best, but another (also non-destructive) way to initialize a hash at declaration from a pair of arrays (of equal size) is:

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "use List::MoreUtils qw(zip); ;; my @players = qw/ barney fred dino/; my @bowling_scores = (195, 205, 30); ;; my %score = zip @players, @bowling_scores; dd \%score; " { barney => 195, dino => 30, fred => 205 }
    (Assumes initializing arrays are of equal size!) See List::MoreUtils::zip().


    Give a man a fish:  <%-{-{-{-<

Re: Working on hash slice
by kcott (Chancellor) on Mar 13, 2019 at 07:44 UTC

    G'day catfish1116,

    Here's a solution which

    • declares and populates the hash in one statement,
    • is non-destructive, and
    • uses no additional modules.
    $ perl -wE ' my @players = qw{Barney Fred Dino}; my @bowled = (195, 205, 30); my %score = map +($players[$_] => $bowled[$_]), 0 .. $#players; say "@players"; say "@score{@players}"; ' Barney Fred Dino 195 205 30

    To hopefully avoid wrapping, I changed "bowling_scores" to "bowled". Other than than, I've used the same variable names and data that you presented.

    In case you were unaware, the + in "map +(..." disambiguates map followed by an expression in parentheses (wanted) from map followed by a list of parameters in parentheses (not wanted). Try it without the + to see the difference; I get player names but no score values, and 8 warning messages.

    Arrays with differing numbers of elements are not addressed here; however, your program should include some sort of validation code to handle that situation.

    — Ken

Re: Working on hash slice
by choroba (Bishop) on Mar 14, 2019 at 00:22 UTC
    Another way to do it, probably a bit slower:
    my %score = map { push @bowled, shift @bowled; $_ => $bowled[-1] } @pl +ayers;
    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1231174]
Approved by marto
Front-paged by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2019-03-20 15:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How do you Carpe diem?





    Results (101 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!