Home > Archive > PERL Beginners > February 2007 > Array of Array
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]
|
|
| Maxou06 2007-02-18, 6:58 pm |
| Hi
does some one know if it possible to do a array of array in perl like
javascript for exemple.
In javascript I was able to push a array into a other array and I was
able to acces the value by doing this.
var value = array[0][0][0];
But in perl when I push a array to a array it just put the variable at
the end of the array.
I will like to acces the data like this my $value=$test[0][0][0]
Can some one tel me if it possible. I looked every wear but I did not
found any answer.
This i my exemple
@tmp=();
push(@tmp,["00","01","02"]);
push(@tmp,["10","11","12"]);
@test=();
push(@test,@tmp);
| |
| Purl Gurl 2007-02-18, 6:58 pm |
| Maxou06 wrote:
> does some one know if it possible to do a array of array in perl like
> javascript for exemple.
#!perl
@Array_of_Array = ([1 .. 3], [4 .. 6], [7 .. 9]);
print $Array_of_Array[1][1];
print "\n\n";
push (@Array_of_Array, [10 ... 12]);
print $Array_of_Array[3][1];
--
Purl Gurl
| |
|
| On Feb 18, 10:18 am, "Maxou06" <soccermania...@hotmail.com> wrote:
> Hi
>
> does some one know if it possible to do a array of array in perl like
> javascript for exemple.
>
> In javascript I was able to push a array into a other array and I was
> able to acces the value by doing this.
>
> var value = array[0][0][0];
>
> But in perl when I push a array to a array it just put the variable at
> the end of the array.
>
> I will like to acces the data like this my $value=$test[0][0][0]
>
> Can some one tel me if it possible. I looked every wear but I did not
> found any answer.
>
> This i my exemple
>
> @tmp=();
> push(@tmp,["00","01","02"]);
> push(@tmp,["10","11","12"]);
>
> @test=();
> push(@test,@tmp);
Hi,
Most of the time, there is not much need to specifically use array
subscripts in Perl. Ususally, the foreach construct or push, pop, etc.
will enable you to do what is desired. That said, sure you can access
data with subscripts.
For example:
use strict;
use warnings;
use English qw( -no_match_vars );
my @array;
push @{$array[0][0]}, 1;
push @{$array[0][0]}, 2;
push @{$array[0][1]}, 3;
push @{$array[0][1]}, 4;
push @{$array[0][2]}, 5;
push @{$array[0][2]}, 6;
push @{$array[1][0]}, 7;
push @{$array[1][0]}, 8;
push @{$array[1][1]}, 9;
push @{$array[1][1]}, 10;
push @{$array[1][2]}, 11;
push @{$array[1][2]}, 12;
my @values = ( 100, 101, 102, 103 );
push @{$array[1][2]}, @values;
$OUTPUT_RECORD_SEPARATOR = "\n";
print $array[0][0][0];
print $array[0][0][1];
print $array[0][1][0];
print $array[0][1][1];
print $array[0][2][0];
print $array[0][2][1];
print $array[1][0][0];
print $array[1][0][1];
print $array[1][1][0];
print $array[1][1][1];
print $array[1][2][0];
print $array[1][2][1];
print $array[1][2][2];
print $array[1][2][3];
print $array[1][2][4];
print $array[1][2][5];
HTH, Ken
| |
| Paul Lalli 2007-02-18, 6:58 pm |
| On Feb 18, 10:18 am, "Maxou06" <soccermania...@hotmail.com> wrote:
> does some one know if it possible to do a array of array in perl like
> javascript for exemple.
No, it's not. What is possible, however, is an array of array
references. This is what you want.
> @tmp=();
> push(@tmp,["00","01","02"]);
> push(@tmp,["10","11","12"]);
Those are both fine. [ ] create a reference to an anonymous array.
> @test=();
> push(@test,@tmp);
That's not fine. You need to push a *reference* to @tmp, not the
values of @tmp itself:
push @test, \@tmp;
This is all documented in the various perldocs:
perldoc perlreftut
perldoc perllol
perldoc perldsc
perldoc perlref
Hope this helps,
Paul Lalli
| |
| Maxou06 2007-02-18, 9:57 pm |
| On Feb 18, 1:01 pm, "Paul Lalli" <mri...@gmail.com> wrote:
> On Feb 18, 10:18 am, "Maxou06" <soccermania...@hotmail.com> wrote:
>
>
> No, it's not. What is possible, however, is an array of array
> references. This is what you want.
>
>
> Those are both fine. [ ] create a reference to an anonymous array.
>
>
> That's not fine. You need to push a *reference* to @tmp, not the
> values of @tmp itself:
>
> push @test, \@tmp;
>
> This is all documented in the various perldocs:
> perldoc perlreftut
> perldoc perllol
> perldoc perldsc
> perldoc perlref
>
> Hope this helps,
> Paul Lalli
Thx But I still not able to do what I want. Can you help me. Here a
example
@tmp=();
push(@tmp , ['000','001','002']);
push(@tmp , ['010','011','012']);
push(@tmp , ['020','021','022']);
push(@tmp , ['030','031','032']);
push @test, \@tmp;
@yo=$test[0];
I will like to have this value : 000 if I do print"$yo[0][0]" but I
have this instead ARRAY(0x189ec24)
Do you know how to do it. Thx
| |
| Maxou06 2007-02-18, 9:57 pm |
| On Feb 18, 8:56 pm, "Maxou06" <soccermania...@hotmail.com> wrote:
> On Feb 18, 1:01 pm, "Paul Lalli" <mri...@gmail.com> wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> Thx But I still not able to do what I want. Can you help me. Here a
> example
>
> @tmp=();
>
> push(@tmp , ['000','001','002']);
> push(@tmp , ['010','011','012']);
> push(@tmp , ['020','021','022']);
> push(@tmp , ['030','031','032']);
>
> push @test, \@tmp;
>
> @yo=$test[0];
>
> I will like to have this value : 000 if I do print"$yo[0][0]" but I
> have this instead ARRAY(0x189ec24)
>
> Do you know how to do it. Thx- Hide quoted text -
>
> - Show quoted text -
| |
| Paul Lalli 2007-02-18, 9:57 pm |
| On Feb 18, 8:56 pm, "Maxou06" <soccermania...@hotmail.com> wrote:
> On Feb 18, 1:01 pm, "Paul Lalli" <mri...@gmail.com> wrote:
>
>
>
>
>
>
>
>
>
> Thx But I still not able to do what I want.
Perhaps not, but it does EXACTLY what you asked for. If what you
asked for is not what you want, you have a problem communicating.
> Can you help me. Here a example
>
> @tmp=();
>
> push(@tmp , ['000','001','002']);
> push(@tmp , ['010','011','012']);
> push(@tmp , ['020','021','022']);
> push(@tmp , ['030','031','032']);
>
> push @test, \@tmp;
>
> @yo=$test[0];
>
> I will like to have this value : 000 if I do print"$yo[0][0]" but I
> have this instead ARRAY(0x189ec24)
>
> Do you know how to do it. Thx-
Yes, I do. You would too, if you'd simply read the documentation I
pointed you to. Why haven't you yet?
my @yo = @{$test[0]};
That's your last fish. Time to learn how to catch them
yourself . . .
Paul Lalli
| |
| Purl Gurl 2007-02-18, 9:57 pm |
| Maxou06 wrote:
> Paul Lalli wrote:
(snipped)
[color=darkred]
> example
> @tmp=();
> push(@tmp , ['000','001','002']);
> push(@tmp , ['010','011','012']);
> push(@tmp , ['020','021','022']);
> push(@tmp , ['030','031','032']);
> push @test, \@tmp;
> @yo=$test[0];
> I will like to have this value : 000 if I do print"$yo[0][0]" but I
> have this instead ARRAY(0x189ec24)
Research and read about reference and dereference,
specifically how those apply to arrays.
#!perl
@tmp=();
push(@tmp , ['000','001','002']);
push(@tmp , ['010','011','012']);
push(@tmp , ['020','021','022']);
push(@tmp , ['030','031','032']);
push (@test, @tmp); # PAY ATTENTION THIS TIME
@yo=$test[0];
print $yo[0][0];
--
Purl Gurl
|
|
|
|
|