For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2006 > store Array in hash ?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author store Array in hash ?
Michael Gale

2006-03-30, 3:57 am

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

2006-03-30, 3:57 am

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
>

Koms Bomb

2006-03-30, 3:57 am

> I have setup a hash like the following:

my $test;
my $setup =3D 'a';

my @data;

push(@data,"test");

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

for(my $c=3D0; $c <=3D $#{@{$test->{$setup}->{'data'}}}; $c++) {
print $test->{$setup}->{'data'}->[$c];
}


Note:
1, it should be "$c <=3D" not "<" because $# is the last index of the array=
,
not the array length.
2, Use "\" to get the reference to @data
3, Use "->" to get the array element.

Sorry for the prevous message sent to you private email address.
John W. Krahn

2006-03-30, 3:57 am

Michael Gale wrote:
> Hello,


Hello,

> I have setup a hash like the following:
>
> my $test;
>
> $test->{$setup}->{'opt'} = "OK";


Or:

my $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;


That should be:

$test->{ $setup }{ data } = [ @data ];

Or:

@{ $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++) {


for ( my $c = 0; $c < @{ $test->{ $setup }{ data } }; $c++ ) {

> print $test->{$setup}->{'data'}[$c];
> }


Or:

for my $item ( @{ $test->{ $setup }{ data } } ) {
print $item;
}


John
--
use Perl;
program
fulfillment
Baskaran Sankaran

2006-03-30, 3:57 am

You can try this.
=20
$test->{$setup}->{'data'} =3D [$data[0], $data[1]];
=20
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.
=20
Baskaran

________________________________

From: Michael Gale [mailto:michael.gale@bluesuperman.com]
Sent: Thu 30/03/2006 13:31
Cc: beginners@perl.org
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'} =3D "OK";
>
> It works fine, now I want to save an array to the hash:
>
> my @data;
>
> push(@data,"test");
>
> $test->{$setup}->{'data'} =3D @data;
>
> Now how do I use it in a for loop later in the script, I tried:
>
> for(my $c=3D0; $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@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Mr. Shawn H. Corey

2006-03-30, 7:57 am

On Wed, 2006-29-03 at 23:45 -0800, John W. Krahn wrote:
> Michael Gale wrote:
>
> Hello,
>
>
> Or:
>
> my $test = { $setup => { opt => 'OK' } };


Or:
my $test{$setup}{opt} = 'OK';

>
>
> That should be:
>
> $test->{ $setup }{ data } = [ @data ];
>
> Or:
>
> @{ $test->{ $setup }{ data } } = @data;


Be aware that these two statements do a top-level copy of @data:
$test{$setup}{data} = [ @data ];
@{ $test{$setup}{data} } = @data;

Whereas this one stores a reference to @data:
$test{$setup}{data} = \@data;

The difference is that in the first two, if @data changes,
$test{$setup}{data} does not. In the second, it will. Consider what
effect you want in your program when you choose between them.

--
__END__

Just my 0.00000002 million dollars worth,
--- Shawn

"For the things we have to learn before we can do them,
we learn by doing them."
Aristotle

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/



John W. Krahn

2006-03-30, 6:57 pm

Mr. Shawn H. Corey wrote:
> On Wed, 2006-29-03 at 23:45 -0800, John W. Krahn wrote:
>
> Or:
> my $test{$setup}{opt} = 'OK';


Did you actually try that?

$ perl -le'my $test{$setup}{opt} = q[OK]; print $test{$setup}{opt}'
syntax error at -e line 1, near "$test{"
Execution of -e aborted due to compilation errors.



John
--
use Perl;
program
fulfillment
Mr. Shawn H. Corey

2006-03-30, 6:57 pm

On Thu, 2006-30-03 at 13:23 -0800, John W. Krahn wrote:
> Did you actually try that?
>
> $ perl -le'my $test{$setup}{opt} = q[OK]; print $test{$setup}{opt}'
> syntax error at -e line 1, near "$test{"
> Execution of -e aborted due to compilation errors.


Oops. Make that:

$test{$setup}{opt} = 'OK';

--
__END__

Just my 0.00000002 million dollars worth,
--- Shawn

"For the things we have to learn before we can do them,
we learn by doing them."
Aristotle

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com