RE: store Array in hash ?



You can try this.

$test->{$setup}->{'data'} = [$data[0], $data[1]];

As you said reference is not going to work, if your array value changes at run time. You can also access specific elements of the array individually, whenever required.

Baskaran

________________________________

From: Michael Gale [mailto:michael.gale@xxxxxxxxxxxxxxxx]
Sent: Thu 30/03/2006 13:31
Cc: beginners@xxxxxxxx
Subject: Re: store Array in hash ?



Hey,

I think I am supposed to use a reference here ? If so I can't because
the data array keeps over written and reused again and again.

I guess I will come up with a different solution.

Michael


Michael Gale wrote:
Hello,

I have setup a hash like the following:

my $test;

$test->{$setup}->{'opt'} = "OK";

It works fine, now I want to save an array to the hash:

my @data;

push(@data,"test");

$test->{$setup}->{'data'} = @data;

Now how do I use it in a for loop later in the script, I tried:

for(my $c=0; $c < $test->{$setup}->{'data'}; $c++) {
print $test->{$setup}->{'data'}[$c];
}

But that just returns:
Can't use string ("2") as an ARRAY ref while "strict refs" in use at
./imap-watch.pl line 380.

Michael


--
To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx
For additional commands, e-mail: beginners-help@xxxxxxxx
<http://learn.perl.org/> <http://learn.perl.org/first-response>




.



Relevant Pages

  • RE: Array de-referencing problem
    ... But I guess it's better to use an array reference? ... Conversation: Array de-referencing problem ... The values of a hash can only be scalars! ...
    (perl.beginners)
  • Re: Complex Objects in Perl
    ... > sub-object is made up of a number of different objects and an array of an ... Objects in Perl are implemented as a reference to an anonymous hash. ...
    (comp.lang.perl)
  • Re: VB-101: Passing Arrays ByVal vs ByRef
    ... changed if an array is passed by Val. ... ' new reference ... As each function creates a new array object, ... 'secondArray' and 'secondArrayCopy'. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: complex data structure
    ... reference to an anonymous one-key hash. ... store, just put them in the array: ... stores a reference to an anonymous 4-element array into scalar variable ...
    (comp.lang.perl.misc)
  • Re: complex data structure
    ... reference to an anonymous one-key hash. ... store, just put them in the array: ... stores a reference to an anonymous 4-element array into scalar variable ...
    (comp.lang.perl)