I am going to guess that you want to print the data back the way it was originally except that it is now sorted on the 3rd column. There are shorter ways to do this, but hopefully this is verbose enough to be helpful.
use strict; use warnings; my @rows; while ( <DATA> ) { chomp; #split row and put in anon array pushed into @rows push @rows, [ split /\|/ ]; } #sort @rows (an Array of Arrays) by column; #@rows looks something like: (ARRAY(0x...),ARRAY(0x...),..) #where ARRAY(0x...) is just a reference to another array. my @sorted = sort { $a->[2] <=> $b->[2] } @rows; #since @sorted is now an array of Arrays sorted #we need to dereference what's in it to print it. foreach my $array_reference ( @sorted ){ #lets join the array pointed to in the array reference # with a "|" # @ dereferences the array #and print print join("|",@$array_reference),$/; } __DATA__ My|Text|5|ACGT My|Text|2|ACGT My|Text|4|ACGT My|Text|3|ACGT My|Text|1|ACGT

As mentioned in the CB you might want to have a look at perlreftut, perldoc perllol, and perldoc perldsc for more on dereferencing (which you had to do to sort in the first place). (there is also references quick reference and intro to references on this site you can look at for more info). Hope this helps.

-enlil


In reply to Re: how to print sorted data by Enlil
in thread 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.