Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: How to code a complex AoH?

by Athanasius (Archbishop)
on Mar 25, 2017 at 15:04 UTC ( [id://1185895]=note: print w/replies, xml ) Need Help??


in reply to How to code a complex AoH?

Hello iatros, and welcome to the Monastery!

Each element of a hash (or of an array, for that matter) must be a scalar value. So if you want to store an array of values in the points slot of a hash, you have to store a pointer to that array:

$student = { ... points => \@points, };

or

$student = { ... points => [@points], };

See perlreftut and perldsc.

Update: To elaborate on stevieb’s point: if the array @points contains the elements ('a', 'b', 'c', 'd'), then the assignment

$student = { ... points => @points, };

is effectively this:

$student = { ... 'points', 'a', 'b', 'c', 'd', };

or, equivalently,

$student = { ... points => 'a', b => 'c', 'd', };

— which explains why the compiler is warning about an odd number of elements in the hash: the last array value ('d') becomes a hash key with no associated value.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: How to code a complex AoH?
by haukex (Archbishop) on Mar 25, 2017 at 15:25 UTC
    points => \@points,

    Actually, that won't work here - it looks like iatros isn't using strict, or has predeclared @points outside of the loop, so that \@points and therefore $$student{points} will always point to the same array, and that array will get overwritten on each iteration of the loop. points => [@points], will work correctly, since it creates a (shallow) copy of the array. (Athanasius, I know you know all this, the explanation is for the benefit of the OP.)

    iatros: You really should Use strict and warnings, and then use my to declare your variables, including inside the loop: my ( $id , $gender , $birthday ... ) = .... Then you can use both of the code examples that Athanasius showed, because then on each iteration of the loop, @points will be a "new" array.

    Update: Tweaked explanation a tiny bit.

Re^2: How to code a complex AoH?
by AnomalousMonk (Archbishop) on Mar 25, 2017 at 18:01 UTC
    ... the last array value ... becomes a hash key with no associated value.

    iatros: Just another | a minor tweak to Athanasius's otherwise excellent ++explanation: The unpaired key will not have no associated value, but rather the undef value (that's where the  '.5' => undef in the OPed dump comes from). This, of course, will probably just lead to more warnings down the line!


    Give a man a fish:  <%-{-{-{-<

Re^2: How to code a complex AoH?
by iatros (Novice) on Mar 30, 2017 at 17:26 UTC
    Thank you for the reply. I hope I've learned my lesson with your help. I will think over the parsing thing. As Larry put it: TIMTOWTDI -HM-

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-24 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found