Hi I need to sort a file in.txt as shown below
05,sometext5 02,sometext2 10,sometext10 03,sometext3
and need the sorted one as below
02,sometext2 03,sometext3 05,sometext5 10,sometext10
i used tell and seek function to swap this. But one of my main aim is performance ,because its going to handle a large files about 500 Mb to 1Gb I wrote the below code. It works fine but takes time for large files. Any suggestions please Regards, blackdragoen
open(IN,"in.txt"); while(<IN>) { if($_=~m/^\n/) { $tell_val=tell(); } if($_=~m/^\d+/) { $has{$&}=$tell_val if($tell_val ne ''); } } open(OUT,">output.txt"); foreach(sort{$a<=>$b}(keys %has)) { seek(IN,$has{$_},0); $line=<IN>; print OUT $line; print $_; }

In reply to Sort lines in a file by blackdragoen

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.