Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^4: How to check if a variable's value is equal to a member of a list of values

by LanX (Canon)
on Mar 25, 2013 at 15:36 UTC ( #1025326=note: print w/ replies, xml ) Need Help??


in reply to Re^3: How to check if a variable's value is equal to a member of a list of values
in thread How to check if a variable's value is equal to a member of a list of values

> I was not previously familiar with the ability to suck an entire array into a hash

it's called "hash slice"! see perldata#Slices

> using this method, how would I set each hash key to a defined static value?

assign a list of same length

DB<156> @array=1..5 => (1, 2, 3, 4, 5) DB<157> @hash{@array} = ('my val') x @array => ("my val", "my val", "my val", "my val", "my val") DB<158> \%hash => { 1 => "my val", 2 => "my val", 3 => "my val", 4 => "my val", 5 => + "my val" }

Cheers Rolf

( addicted to the Perl Programming Language)

UPDATE

for completeness TIMTOWTDI:

$hash{$_}="my val" for @array

has less of a memory footprint.

and

 my %hash = map { $_ => "my val" } @array;

is DRYer and makes sure the whole hash is reinitiated.


Comment on Re^4: How to check if a variable's value is equal to a member of a list of values
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2015-08-01 01:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (285 votes), past polls