Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: initialize all values in a hash

by DrHyde (Prior)
on May 18, 2012 at 10:35 UTC ( #971266=note: print w/ replies, xml ) Need Help??


in reply to initialize all values in a hash

You appear to be very confused!

my $maxBits=60 is not a hash reference. 60 is a plain old scalar.

my %maxBits=60 is indeed wrong, because %maxBits is a hash, but 60 is again as plain old scalar. Perl will *try* to Do The Right Thing and assume that the right hand side is a list of key/value pairs, but it can't because there's an odd number of elements in the list (it gets treated as a list with one element). Hence the warning "Odd number of elements in hash assignment". It then, after warning you, extends the list by appending an undef, creating a hash that looks like this:

60 => undef

that is, the hash has a single key in it (60) whose value is undef.

Finally, %maxBits=map {$_=>60};. This is a syntax error, because map needs a list of values to work with.

I recommend that you stop and read Learning Perl (the Llama book). IIRC (and I admit I've not looked at it in the last several years) it includes a good tutorial on data types - scalars, arrays, hashes. I don't think it includes much on references, but once you've read and understood the Llama you should be able to cope with perlreftut, perldsc, and all the other tutorials that come with perl and, if you still need it, the documentation for the map function.


Comment on Re: initialize all values in a hash
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2014-08-23 17:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (175 votes), past polls