access a 2D array column by column

E

ela

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?


use warnings;

my ($rankfile) = @ARGV;
open (FP,$rankfile);
my @cells = (); my @AoA = ();
while ($line=<FP>) { chomp $line; @cells = split /\t/, $line;
push @AoA, [ @cells ];
}

%trials = ();
%sTrials = ();
for $i ( 0 .. $#AoA ) {
for $ik ( 0 .. $#{ $AoA[$i] } ) {
$trials{$ik} = $AoA[$i][$ik];
print $ik, "\t", $trials{$ik} , "\t", $AoA[$i][$ik]; <STDIN>;
}
}
 
J

Jürgen Exner

ela said:
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?
use warnings;

my ($rankfile) = @ARGV;
open (FP,$rankfile);
my @cells = (); my @AoA = ();
while ($line=<FP>) { chomp $line; @cells = split /\t/, $line;
push @AoA, [ @cells ];
}

%trials = ();
%sTrials = ();
for $i ( 0 .. $#AoA ) {
for $ik ( 0 .. $#{ $AoA[$i] } ) {
$trials{$ik} = $AoA[$i][$ik];
print $ik, "\t", $trials{$ik} , "\t", $AoA[$i][$ik]; <STDIN>;
}
}

You need to change your algorithm to have the outer loop iterate over
the columns and the inner loop over the rows.

jue
 
E

ela

Jürgen Exner said:
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]
 
J

Jürgen Exner

ela said:
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]

So, where is the problem?

for $col in (0..1) {
for $row in (0..2) {
print $arr[$row][$col]
}
}

jue
 
J

Justin C

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::parseExcel is
useful:


#!/usr/bin/perl

use warnings;
use strict;

use Spreadsheet::parseExcel;

my $parser = Spreadsheet::parseExcel->new();
my $wb = $parser->parse($ENV{HOME} . '/some.xls');

die unless defined $wb;

for my $ws ( $wb->worksheets() ) {
my ($row_min, $row_max) = $ws->row_range();
my ($col_min, $col_max) = $ws->col_range();

for my $col ($col_min .. $col_max) {
for my $row ($row_min .. $row_max) {
my $cell = $ws->get_cell($row, $col);
print $cell->value(), "\n";
}
}
}

__END__

But please read and understand the comments others have posted.


Justin.
 
J

John W. Krahn

Tad said:
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;

Or just copy the list directly:

while (my $line = <$FP>) {
chomp $line;
push @AoA, [ split /\t/, $line ];



John
 
W

Willem

John W. Krahn wrote:
) Or just copy the list directly:
)
) while (my $line = <$FP>) {
) chomp $line;
) push @AoA, [ split /\t/, $line ];

Or just do it all at once:

my @AoA = map { chomp; [ split /\t/ ] } <$FP>;

Anyone want to go for an eagle?


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
 
E

ela

Tad McClellan said:
That is extremely unlikely. :-(

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.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top