in reply to Regarding Hashes of Arrays
$hash{$id} = (1,2,3); will set $hash{$id} = 3 (the last element in the list).
and $hash{$id} = @temp; will set $hash{$id} = 3 (the length of @temp).
You cannot literally make a hash with arrays as the values in Perl, you need array-references:
Note that \(1,2,3) will make a list of references to 1, 2 and 3, so don't use that.$hash{$id} = [1,2,3]; # [ ... ] creates an anonymous array ref @temp = (1,2,3); $hash{$id} = \@temp; # \ makes an explicit reference to @temp
If you want to read one of the values from the array ref you do:
$x = ${$hash{$id}}[0]; # or $x = $hash{$id}[0]; # or $x = $hash{$id}->[0];
I like the last syntax best, because it makes it clear that there is a reference (more or less equivalent to a "pointer" in C speak) involved.
If you want to whole list of values from the array, you can do:
perlref has a lot of info about this.my @values = @{$hash{$id}};
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Regarding Hashes of Arrays
by hardburn (Abbot) on Aug 10, 2004 at 20:16 UTC | |
by Joost (Canon) on Aug 10, 2004 at 20:30 UTC | |
by hardburn (Abbot) on Aug 10, 2004 at 20:51 UTC | |
by Joost (Canon) on Aug 10, 2004 at 20:59 UTC |