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

Re^2: using hash to compare with string and print the string

by WoodyWeaver (Monk)
on May 16, 2019 at 13:38 UTC ( [id://11100082]=note: print w/replies, xml ) Need Help??


in reply to Re: using hash to compare with string and print the string
in thread using hash to compare with string and print the string

I wanted to comment on style here. Notice that hippo writes
my @array = qw/b d a c/; my %hash = ( a => 200, b => 100, c => 30, d => 40 );
while Thanos offers
my @keys = qw(b d a c); my @values = qw(200 100 30 40); my %hash; @hash{@keys} = @values;
I didn't know this trick with hash slicing, and I appreciate the pointer. It is very helpful to learn new things.

Stylistically, the latter is smart, the former is pleasant. If I can invoke Henry Koster's _Harvey_, "'In this world, Elwood, you must be oh so smart or oh so pleasant.' Well, for years I was smart. I recommend pleasant. You may quote me." These two aren't identical, I think. Note $hash{'a'} is different in the two samples (30 and 200). There are times when clever is better (more concise, more elegant, more insightful, golfing) and there are times when obvious is better. If I'm just writing for myself, I lean toward clever. But when I'm trying to get something done, or writing for myself in six months, I'm with Elwood.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-26 07:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found