Hello,

million of lines still probably fit in memory.. Note that $#{$aoa[0]} assumes all lines are of the same length as you said.

use strict; use warnings; my @aoa; while (<DATA>) { chomp; push @aoa,[split '',$_]; } foreach my $col(0..$#{$aoa[0]}){ print "Column $col: ", (join ' ',map { $aoa[$_][$col] } 0..$#aoa), "\n"; } __DATA__ ACATCACCTC ACATCACCTC ACATCACCTC ACATCACCTC # out Column 0: A A A A Column 1: C C C C Column 2: A A A A Column 3: T T T T Column 4: C C C C Column 5: A A A A Column 6: C C C C Column 7: C C C C Column 8: T T T T Column 9: C C C C

L*

UPDATE if really care memory you can try the following (*untested*)approach:

# pseudocode!! # analize first line my $line = <$fh>; chomp $line; # compute last index of the future array (or future string? be aware o +f possible off one errors!!); my last = length $line - 1; # rewind the filehandle seek $fh,0,0; sub get_column{ my $col = shift; my $line = shift; if($col==0){$line=~/^(.)/} elsif($col==$last){$line=~/(.)$/} else{ $line=~/.{$col-1}(.)/} # or $last - $col? return $1; } while (<$fh>){ chomp; print get_column(3,$_) }
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

In reply to Re: Faster and more efficient way to read a file vertically -- updated by Discipulus
in thread Faster and more efficient way to read a file vertically by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.