No, I want to use Hash of Arrays.
I want to define the hash in a better manner say something similar to
the following way.

my %ModelPath =           {
                      "2800" => [EMAIL PROTECTED],
                      "4345" => [EMAIL PROTECTED]
                    };
But by doing so I am not getting the values for the respective keys.

-----Original Message-----
From: Tim Johnson [mailto:[EMAIL PROTECTED]
Sent: Wednesday, May 05, 2004 11:01 AM
To: Suresh Pasupula (WT01 - EMBEDDED & PRODUCT ENGINEERING SOLUTIONS);
[EMAIL PROTECTED]
Subject: RE: [ Hash of Arrays]



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>



Confidentiality Notice

The information contained in this electronic message and any attachments to this 
message are intended
for the exclusive use of the addressee(s) and may contain confidential or privileged 
information. If
you are not the intended recipient, please notify the sender at Wipro or [EMAIL 
PROTECTED] immediately
and destroy all copies of this message and any attachments.

--
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