access a 2D array column by column

Discussion in 'Perl Misc' started by ela, Nov 25, 2011.

  1. ela

    ela Guest

    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>;
    }
    }
     
    ela, Nov 25, 2011
    #1
    1. Advertising

  2. "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?

    >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
     
    Jürgen Exner, Nov 25, 2011
    #2
    1. Advertising

  3. ela

    ela Guest

    "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, Nov 25, 2011
    #3
  4. "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]


    So, where is the problem?

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

    jue
     
    Jürgen Exner, Nov 25, 2011
    #4
  5. ela

    Justin C Guest

    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::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.

    --
    Justin C, by the sea.
     
    Justin C, Nov 25, 2011
    #5
  6. 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;


    Or just copy the list directly:

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



    John
    --
    Any intelligent fool can make things bigger and
    more complex... It takes a touch of genius -
    and a lot of courage to move in the opposite
    direction. -- Albert Einstein
     
    John W. Krahn, Nov 25, 2011
    #6
  7. ela

    Willem Guest

    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
     
    Willem, Nov 25, 2011
    #7
  8. ela

    ela Guest

    "Tad McClellan" <> wrote in message
    news:...
    > Justin C <> wrote:
    >
    >> But please read and understand

    >
    >
    > That is extremely unlikely. :-(
    >
    >> the comments others have posted.


    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.
     
    ela, Nov 28, 2011
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    2
    Views:
    610
    Guest
    Nov 10, 2003
  2. Jim Hammond
    Replies:
    1
    Views:
    3,682
    bruce barker
    Nov 26, 2003
  3. Finn Stampe Mikkelsen

    Column count / Access GridView Column after DataBind

    Finn Stampe Mikkelsen, Aug 19, 2010, in forum: ASP .Net
    Replies:
    4
    Views:
    3,297
    Finn Stampe Mikkelsen
    Aug 20, 2010
  4. Gregory Rampton

    Convert an MS Access Yes/No column to a checkbox column in C# datagrid

    Gregory Rampton, Aug 6, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    238
    Gregory Rampton
    Aug 6, 2003
  5. fatted
    Replies:
    2
    Views:
    162
    James Willmore
    Oct 23, 2003
Loading...

Share This Page