Perl: the Markov chain saw | |
PerlMonks |
RE: Re: Using an array in a hashby BlaisePascal (Monk) |
on Aug 08, 2000 at 04:51 UTC ( [id://26714]=note: print w/replies, xml ) | Need Help?? |
To elaborate on that second part...
When you say %hash = (a => "foo", b => "bar") perl treats the "=>" operator as a synonym for ",", so this really looks like %hash = (a,"foo",b,"bar"). Perl knows to treat a list assigned to a hash as key/value pairs, so this works right. In your example, where you tried %hash = (a => (foo,bar, baz)), perl treats the right hand side as (a,(foo,bar,baz)), which is "flattened" into (a,foo,bar,baz), which is then assigned to %hash, yielding %hash{a} = "foo" and %hash{bar} = "baz", which isn't what you wanted. The suggestions to use references will do what you want.
In Section
Seekers of Perl Wisdom
|
|