in reply to saving nested arrays in a hash
There are a a lot of bugs in your example code. Please add 'use warnings;' at the beginning, you will find out that a lot of errors get noticed that way. Also 'use strict;' is advisable
To answer your question, the problem is that your array references always point to the same data structure. The reason is that you use global variables. To correct that you could either use 'my' to create a new variable every time, i.e. my @array1= ();, or you could make sure that you copy the values, i.e. $hash{$_} = [ [@array1], ... ];
But your script has more problems than that. For example:
$array1=();. The $ should be a @, if you had warnings on, perl would have told you
array1_ref = \@array1;. A '$' is missing in front of the array1_ref. Again perl would have told you with warnings on
Also the clearing of @array1 and @array2 should be before the while loop, if I get your intention right
You could add a line 'use Data::Dumper;' to the beginning of your program and then for example print Dumper(\%hash); at the end to find out how the data structure looks in reality. Data::Dumper ist the best available debugging tool you can find in perl on short notice ;-)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: saving nested arrays in a hash
by maayanster (Initiate) on Aug 26, 2010 at 16:33 UTC | |
by jethro (Monsignor) on Aug 26, 2010 at 18:01 UTC | |
by TomDLux (Vicar) on Aug 26, 2010 at 17:38 UTC |