access a 2D array column by column

To access a spreadsheet file column by column, I use the 2D array @AoA to
store the contents. However, I don't know how to state in the for loop
because common perl tutorials assume access row by row. What should I
replace $#AoA with?

Advertisements

"ela" <> wrote:
>To access a spreadsheet file column by column, I use the 2D array @AoA to
>store the contents. However, I don't know how to state in the for loop
>because common perl tutorials assume access row by row. What should I
>replace $#AoA with?

Advertisements

"Jürgen Exner" <> wrote in message
news:...
> "ela" <> wrote:
>>To access a spreadsheet file column by column, I use the 2D array @AoA to
>>store the contents. However, I don't know how to state in the for loop
>>because common perl tutorials assume access row by row. What should I
>>replace $#AoA with?
>
> Do you know what $#AoA is? Why do you think that picking some other
> number would change the logic of your algorithm from columns of rows to
> rows of columns?

The problem is I don' t know what the last index for a 2D array refers to...
> You need to change your algorithm to have the outer loop iterate over
> the columns and the inner loop over the rows.

Yes. I should iterate over the columns and then rows. That's why I first
store the file content into a 2D array. Say, if I have something like:

array of size 3 by 2

15 6
3 9
7 18

then if I have to access as 15, 3, 7 and then 6,9,18 I should move the first
index, e.g. arr[0][0], arr[1][0], and arr[2][0]; then
arr[0][1], arr[1][1] and arr[2][1]

"ela" <> wrote:
>
>"Jürgen Exner" <> wrote in message
>news:...
>> "ela" <> wrote:
>>>To access a spreadsheet file column by column, I use the 2D array @AoA to
>>>store the contents. However, I don't know how to state in the for loop
>>>because common perl tutorials assume access row by row. What should I
>>>replace $#AoA with?
>>
>> Do you know what $#AoA is? Why do you think that picking some other
>> number would change the logic of your algorithm from columns of rows to
>> rows of columns?
>
>The problem is I don' t know what the last index for a 2D array refers to...

In Perl there is no such thing as a 2D array. As AoA indicates you got
an _A_rray _o_f (references to) _A_rrays.
>> You need to change your algorithm to have the outer loop iterate over
>> the columns and the inner loop over the rows.
>
>Yes. I should iterate over the columns and then rows. That's why I first
>store the file content into a 2D array. Say, if I have something like:
>
> array of size 3 by 2
>
>15 6
>3 9
>7 18
>
>then if I have to access as 15, 3, 7 and then 6,9,18 I should move the first
>index, e.g. arr[0][0], arr[1][0], and arr[2][0]; then
>arr[0][1], arr[1][1] and arr[2][1]

On 2011-11-25, ela <> wrote:
> To access a spreadsheet file column by column, I use the 2D array @AoA to
> store the contents. However, I don't know how to state in the for loop
> because common perl tutorials assume access row by row. What should I
> replace $#AoA with?

This spreadsheet you have, if it's Excel then Spreadsheet:arseExcel is
useful:

Tad McClellan wrote:
> ela<> wrote:
>>
> Since you do not use @cells outside of the while loop, you should
> scope @cells to the while loop's block rather than scoping it
> to the whole file like that.
>
>
>> while ($line=<FP>) { chomp $line; @cells = split /\t/, $line;
>
> while (my $line =<$FP>) {
> chomp $line;
> my @cells = split /\t/, $line;
>
>
>> push @AoA, [ @cells ];
>
>
> Now that @cells is properly scoped, you can take a reference to
> it directly rather than having to copy it to an anonymous array:
>
> push @AoA, \@cells;

SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

Trying hard to follow the etiquette. Sometimes when a program is used only
once I somehow become lazy and don't follow strictly... sorry again for
making you disappointed but I always remember when I become rich, this group
should be the first to which I should donate.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!