Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping through double arrays

Status
Not open for further replies.

LuckyKoen

Programmer
Feb 14, 2005
57
BE
I have some trouble with showing data in an array. I have 2 arrays and I put one array in another :

my @array1;
my @arraygroup
push (@array1, "a");
push (@array1, "b");
push (@arraygroup, @array1);

This seems to be no problem for perl, when I use the following loop I get " ab " as a result :

foreach my $array (in $arraygroup) {
print $array;
}

I want to address each item in array1 separately though, so I try the following :

foreach my $array (in $arraygroup) {
print $array[0];
}

Which doesn't work since I defined $array instead of @array, so I change this :

foreach my @array (in $arraygroup) {
print $array[0];
}

This won't work either because foreach requires a $variable to be declared and won't take an array instead. How should I get this thing to work ?

Also, are there better alternatives to double arrays in Perl ? I use these often in Java but I haven't found much about them in Perl yet. I'm wondering why they aren't that common.
 
I didn't understand what is it that you want to do but i 'll give you some hints
Code:
If you set things like you did

my @array1;
my @arraygroup[red];[/red]  #you forgot the ';'
push (@array1, "a");
push (@array1, "b");
push (@arraygroup, @array1);

then you have 

$array1[0] = "a"
$array1[1] = "b"
$arraygroup[0] = "a"
$arraygroup[1] = "b"

so if you want to print them just say 

print $array1[0];
print $arraygroup[1];
.....

if you want to just go through them do it like this

foreach (@array1){
   print $_,"\n";    #which gives you 'a' and on the next line 'b'
}

or 

foreach my $element (@arraygroup){
   print $element;
}

such thing as 
[red]foreach my @array (in $arraygroup) {
    print $array[0];
}[/red] [b]does not exists[/b]

if you want you can do something like this

my @array = @arraygroup;

and then 

print $array[0];  #which gives 'a'

or 

foreach (@array){
   print $_,"\n";
}


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
Technically, an array can't contain another array as one of its elements, so pushing @array1 into @arraygroup isn't going to do what you want it to. It is, however, perfectly valid syntax, which is why it won't give you an error, though I won't go into that right now.

Array elements can only be scalars. However, a reference to an array is a perfectly legitimate scalar to hold in an array. Thus, to get @array1 into @arraygroup is like this (the backslash is the `reference of' operator):
Code:
push (@arraygroup, \@array1);

Now, you can loop through @arraygroup like any other array (Note: there's no `in' operator in Perl):
Code:
foreach my $element ( @arraygroup ) {
   # do something
}

Now for the `do something' bit! - normally, to access the first element of an array @array, you'd do this: $array[0]. However, when you have a reference to an array, rather than an actual array, you need to dereference using the `->' operator, i.e.:
Code:
foreach my $element ( @arraygroup ) {
   print $element->[0];
}
That will now print the first element in each array contained in @arraygroup. To print every element in every array, do this:
Code:
foreach my $outer_element( @arraygroup ) {
   foreach my $inner_element( @{ $outer_element } ) {
      print $inner_element;
   }
}

Note that now we use @{ ..arrayref.. } to convert an array reference into an array.

To read more on complex data structures in perl, google for `perllol'.

Hope some of that makes sense!
 
Thanks Ish, that answer couldn't have been more clear :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top