Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Working on hash slice

by NetWallah (Canon)
on Mar 12, 2019 at 18:48 UTC ( [id://1231177]=note: print w/replies, xml ) Need Help??


in reply to Working on hash slice

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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-03-28 21:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found