leegold has asked for the wisdom of the Perl Monks concerning the following question:

Trying to get facilty with hash arrays and I am having a nasty time. Please see the snippet below, Btw, I'm working out of A.L. Johnson's "Elements of Programming with Perl" pp.146. Could a you beakdown/explain the code, I'll tell you specifically where I'm stuck after the snippet:

students.dat ------------ Bill Jones : 2 : 35 Anne Smith : 3 : 41 $students {$fields[0]}->[$fields[1]]=$fields[2]

So looks like, $student is an anonyous hash reference. $fields[0] is the key (ie. key is student name). Taken in total the left side of the arrow is an anonymous array reference. It's the right side of the arrow I don't understand, what's the "=" doing.....Heck, I don't understand. Could a monk gently break it down for me? Help appreciated.

Lee

Replies are listed 'Best First'.
Re: help w/hash arrays
by NetWallah (Canon) on May 23, 2004 at 20:02 UTC
    Things would be easier to understand if you replaced the @fields array elements with equivalent scalar values.

    Assume that the following has been entered:

    my ($x,$y,$z) = @fields; # to simplify matters # Now, your statement can be written as : $students{$x}->[$y] = $z;
    %students is NOT an anonymous hash ref - it has a name - students. It is not a ref (it is a hash), and its values are references to anonymous arrays.

    So, if you asked for $students{$x} , you would get a refernence to an anonymous array. You de-reference this with the optional "->" operator, then index the array with [$y]. Finally, you assign $z to the indexed array.

    Hope this lifts the fog somewhat.

    Offense, like beauty, is in the eye of the beholder, and a fantasy.
    By guaranteeing freedom of expression, the First Amendment also guarntees offense.
Re: help w/hash arrays
by Zaxo (Archbishop) on May 23, 2004 at 19:52 UTC

    You're looking at it with the wrong precedence in mind. That is just assignment with the lhs $students {$fields[0]}->[$fields[1] and the rhs $fields[2]. The dereference arrow is optional, so that could as well be written $students{$fields[0]}[$fields[1]] = $fields[2]; which is probably more readable.

    After Compline,
    Zaxo

Re: help w/hash arrays
by TGI (Parson) on May 24, 2004 at 00:09 UTC

    Others have given some good explainations, no need to repeat them. But here are two tips: Data::Dumper is your friend, and you can use print statements to help tease apart these statements.

    1. Data:Dumper is wonderful for figuring out data structures. Just add a
      use Data::Dumper;
      at the start of your code, and then add a line like:
      print Dumper(\%students);
      You'll get a nice printout of the data structure and its contents. By the way, the backslash returns a reference to the variable it precedes.
    2. You can also try printing things to find out what they are. If you print a reference, you'll get something like SCALAR(0xda28) with a datatype and an address/uid.


    TGI says moo