Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://971266]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2017-07-21 06:48 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (319 votes). Check out past polls.