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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array of Array question 2

Status
Not open for further replies.

f97tosc

Programmer
Joined
Nov 21, 2006
Messages
1
Location
US
Hi, I have some problems with an Array of Array.

The number of rows are known from the beginning, but the number of elements in each row is variable (and could be zero). For example, I could initialize this structure as:

Code:
my @AoA = ([],[],[0,1],[],[],[2,3,4]);

Now the question is, how do I loop through this array, without keeping separate records of the number of elements in each row? If the row is empty it should just be skipped. This does not work:

Code:
for ($i=0;$i<6;$i++)
{
foreach $j @AoA[$i]
...
}}

Please help! Thanks.
 
Code:
my @AoA = ([],[],[0,1],[],[],[2,3,4]);

for my $i (0..5)
{
next unless @{$AoA[$i]};
foreach my $j (@{$AoA[$i]}) {
   print "$j\n";
}
}

- Kevin, perl coder unexceptional!
 
All you need here is a better understanding of references in perl, and how to dereference.

Code:
# This is your main array.
my @AoA = ([],[],[0,1],[],[],[2,3,4]);

# This is an element of your main array.
# It contains a reference to another array.
my $arrayRef = $AoA[2]; # Equals [0,1];

# This dereferences your subarray.
my @array = @$arrayRef; # Equals (0,1);

# This does the previous two steps in a single line.
my @array = @{$AoA[2]}; # Equals (0,1);

# This counts the number of elements in your third array.
# Note how it is used in a scalar context.
my $count = @{$AoA[2]}; # Equals 2

# This access the second element of the sub array.
my $element = $AoA[2][1];

Hope this helps.
 
Just as a quick follow up. There are two approaches to this type of problem. One is to loop through the array by index, the other is to loop through by element. Which method you choose depends on your specific needs. Each of the follow three methods do the exact same thing.

Code:
my @AoA = ([],[],[0,1],[],[],[2,3,4]);

print "By Index (using iterator)\n";
for (my $i=0; $i<@AoA; $i++) {
	print "[ ";
	for (my $j=0; $j<@{$AoA[$i]}; $j++) {
		print "$AoA[$i][$j] ";
	}
	print "]\n";
}

print "By Index (using foreach)\n";
for my $i (0..$#AoA) {
	print "[ ";
	for my $j (0..$#{$AoA[$i]}) {
		print "$AoA[$i][$j] ";
	}
	print "]\n";
}

print "By Element\n";
foreach my $array (@AoA) {
	print "[ ";
	foreach my $element (@$array) {
		print "$element ";
	}
	print "]\n";
}

Output:
Code:
By Index (using iterator)
[ ]
[ ]
[ 0 1 ]
[ ]
[ ]
[ 2 3 4 ]
By Index (using foreach)
[ ]
[ ]
[ 0 1 ]
[ ]
[ ]
[ 2 3 4 ]
By Element
[ ]
[ ]
[ 0 1 ]
[ ]
[ ]
[ 2 3 4 ]

Of course, you can mix up the use of different types of looping. You could choose to loop through the main array by index, and the subarrays by element like KevinADC did. Or you can do the inverse. It all depends on your preference.

Also note that "for" and "foreach" are synomymous in perl. I personally prefer to use "foreach" when looping through elements, and "for" when interating.

Enjoy.
 
With my 'design patterns pedant' head on, I'd have to say that foreach is effectively an iterator and for isn't, but that's just splitting hairs. Especially as foreach in perl is just a bit of syntactic sugar.

But seriously, the whole purpose of the iterator pattern is to make it easier to code something that iterates over all elements of a collection or array. I'm with MillerH on this one, use foreach unless you need the $i value inside the loop to do something other than index the array.

As his final example shows, the resulting code code is much simpler, cleaner, and easier to read.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::PerlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top