First I'll show you ONE of the right ways:

############################

use strict;
use warnings;

#declare the arrays
my @one = ("abc", "def");
my @two = ("ABC", "DEF");

#initialize the hash (note the parens)
my %ModelPath = ();

#add the arrays to the hash
$ModelPath{1} = [EMAIL PROTECTED];
$ModelPath{2} = [EMAIL PROTECTED];

#retrieve the values and assign to
[EMAIL PROTECTED] by dereferencing the hash key
my @temp = @{$ModelPath{1}};

#retrieve just one value
my $temp2 = $ModelPath{1}->[0];
 
#############################

You have to remember that when you add an array via a structure like
this:

$ModelPath{1} = [EMAIL PROTECTED];

Or

$ModelPath{1} = [EMAIL PROTECTED];

You are assigning a SCALAR REFERENCE to the hash key, not the array
itself (although there is some magic there that can be used sometimes).
Therefore, you must treat the hash key the same way you would any other
reference, not as if it was an array by itself.  Sometimes it's easier
to do this:

#assign the reference to a temp variable
my $temp = $ModelPath{1};

#retrieve the first element
my $firstElement = $temp->[0];

#dereference the array and assign to a new array
my @fullArray = @{$temp};





-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, May 04, 2004 9:58 PM
To: [EMAIL PROTECTED]
Subject: [ Hash of Arrays]

----------------<snip>-------------------

For eg. I have two arrays.

my @one = ("abc", "def");

my @two = ("ABC", "DEF");

Now I will initialize a hash in the following way.

my %ModelPath = {};

$ModelPath{"1"} = [EMAIL PROTECTED];

$ModelPath{"2"} = [EMAIL PROTECTED];

----------------<snip>-------------------

When I retrieve the value from hash I have to do in the following way,
if I am not wrong.

my @temp = $ ModelPath{"1"}; #but here the temp[0] contains the whole
array "one" rather than individual "$one[0]".

----------------<snip>-------------------

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to