array of arrays: searching for wisdom ...

A

Alythh

hi people,
I've some troubles trying to access an array of arrays:
while using the format:

for $i ( 0 .. $#AoA ) {
$row = $AoA[$i];
for $j ( 0 .. $#{$row} ) {
print "element $i $j is $row->[$j]\n";
}
}

surely works, I was wandering if there is a way to scan @AoA for its
$i-th row, and returning it as @i_row, without rebuilding it (by
pushing one-by-one $row->[$j], as in previous example):
it is both an efficiency worry, and a styilistic one: that code is
awful!

Thanks for your help

Alessandro Magni
 
T

Tore Aursand

Alythh said:
for $i ( 0 .. $#AoA ) {
$row = $AoA[$i];
for $j ( 0 .. $#{$row} ) {
print "element $i $j is $row->[$j]\n";
}
}

surely works, I was wandering if there is a way to scan @AoA for its
$i-th row, and returning it as @i_row, without rebuilding it (by
pushing one-by-one $row->[$j], as in previous example):
it is both an efficiency worry, and a styilistic one: that code is
awful!

Access it directly (and dereference at the same time);

my $i = 0;
my @i_row = @{$AoA[$i]};
 
B

Brian McCauley

Alythh said:
hi people,
I've some troubles trying to access an array of arrays:
while using the format:

for $i ( 0 .. $#AoA ) {
$row = $AoA[$i];
for $j ( 0 .. $#{$row} ) {
print "element $i $j is $row->[$j]\n";
}
}

You should always declare all variables as lexically scoped in the
smallest applicable scope unless you have a reason to do otherwise.
surely works, I was wandering if there is a way to scan @AoA for its
$i-th row, and returning it as @i_row, without rebuilding it (by
pushing one-by-one $row->[$j], as in previous example):

There is no push in the previous example.

The whole of the array referenced by the ARRAYref $row can be addressed
as @$row.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top