I am trying to understand how sort works so I took
the following file:
My|Text|5|ACGT
My|Text|2|ACGT
My|Text|4|ACGT
My|Text|3|ACGT
My|Text|1|ACGT
and I want to sort it using the thrid column (numbers)
as reference. Here is what I've tried to work with:
#!/usr/bin/perl my @rows = (); open FH, "LINE" or die $!; open FHO, ">SORT"; while (<FH>) { chomp; push @rows, [ split /\|/ ]; #I guess this should work with any separators,e.g \t as well? } close FH; #my @sorted = sort { $a->[2] <=> $b->[2] } @rows; #printf "%g\t%g\t%i\t%g\n", sort { $a->[2] <=> $b->[2] } @rows; #foreach ( sort {$a->[2] <=> $b->[2] } @rows ) { #printf "$_ \n" } print map {$_->[2] . "\n" } sort { $a->[2] <=> $b->[2]} @rows; #print map {$_ . "\n" } sort { $a->[2] <=> $b->[2]} @rows; exit
The print map command will only print out the sorted column
with numbers (and not the other columns). I've also tried to
print the data out using
for($i = 0; $i <4; $i++) { print map {$_->[$i] . "\n" } sort { $a->[2] <=> $b->[2]} @rows;
but it printed out the columns on top of each other. I would appreciate your help. I want especially to understand
how this works.
Thank you,
Ioana

In reply to how to print sorted data by ioana

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.