Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
I have 2 arrays of arrays (let's call them @AoA_first and @AoA_second that have same amount of lines and columns. What I want to do is read through each of them, and print in a file, each column of each array of arrays, separated by tabs.
I managed to do so with the following code, but the thing is that it is very slow and there has to be a faster way that I am not aware of...
Please advise!
for $i ( 0 .. $#AoA_first ) { $row_first = $AoA_first[$i]; $row_second = $AoA_second[$i]; open TEMP, ">TMPFILES/$i.tmpfile"; for $j ( 0 .. $#{$row_first} ) { print TEMP "$row_first->[$j]\t$row_second->[$j]\n"; } close TEMP; } }

I think it is slow for me because it reads through every element of every column of each of the two array of arrays, but I can't think of another way to "dump" the two columns side by side into a new file...

Replies are listed 'Best First'.
Re: Efficient way to print 2 columns from 2 Array of Arrays next to each other
by BrowserUk (Patriarch) on Oct 25, 2016 at 16:56 UTC

    How about:

    open TEMP, ">TMPFILES/$i.tmpfile" or die $!; printf TEMP "%s\t%s\n", $AoA_first[ $_ ], $AoA_second[ $_ ] for 0 .. $ +#AoA_first;

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Thank you very much!
Re: Efficient way to print 2 columns from 2 Array of Arrays next to each other
by Laurent_R (Canon) on Oct 25, 2016 at 17:17 UTC
    BrowserUk has given a good solution, but let me come back to your code to point out your errors.

    You are opening the TEMP filehandle for writing n times, with n being the number of elements of one of your arrays. And you're writing all your data n times, each time clobbering what you've written in the previous iteration.

    From your description of the problem, you don't need nested loops, you only need one loop, because the same loop variable ($i in the code below) can access the corresponding items in both arrays.

    open my $TEMP, ">", "TMPFILES/$i.tmpfile" or die "cannot open output f +ile $!"; for $i ( 0 .. $#AoA_first ) { print $TEMP "$row_first->[$i]\t$row_second->[$i]\n"; } close $TEMP;
    The code above is essentially the same, in a less concise form, as BrowserUk's solution.
Re: Efficient way to print 2 columns from 2 Array of Arrays next to each other
by BillKSmith (Monsignor) on Oct 25, 2016 at 22:38 UTC
    It is somewhat shorter, and probably faster, to use a module for the inner loop.
    use strict; use warnings; use List::MoreUtils qw(pairwise); my @AoA_first = ( [qw(a b c)], [qw(d e f)], ); my @AoA_second = ( [qw(1 2 3)], [qw(4 5 6)], ); for my $i ( 0 .. $#AoA_first ) { my $row_first = $AoA_first[$i]; my $row_second = $AoA_second[$i]; open my $TEMP, '>', "TMPFILES/$i.tmpfile" or die "Cannot open $i.tmpfile"; print {$TEMP} pairwise {"$a\t$b\n"} @$row_first, @$row_second; close $TEMP; }
    Bill