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

Dear Monks I have a textfile, and I want to sort it based on the value of the first set (each line have three sets separate by a space)
#My Input text file: dec.x.bc.00041669 1067503 1067501 dec.x.bc.00019580 1067503 1067501 dec.x.bc.00016113 1067503 1067501 #I want my Output textfile: dec.x.bc.00016113 1067503 1067501 dec.x.bc.00019580 1067503 1067501 dec.x.bc.00041669 1067503 1067501 #this is my code @filenames=@ARGV; foreach my $file (@filenames) { open (INFILE, $file) || die ("Can't open file $file$!"); chomp(my @resultArray = map { /^\s*$/ ? () : $_ } <INFILE>); open(OUTFILE, ">$file.formatted"); sort { $a<=>$b } @resultArray; print Dumper \@resultArray; foreach my $line(@resultArray){ my ($ID, $ans1, $ans2) = split( / /, $line ); #do something; print OUTFILE "$lineResults\n"; } close OUTFILE; }
I want to do something with a sorted array, and I guess after "I do something", each line will be copied in the output textfile already sorted eh? But is not sorting!
What is the problem?
Thanks

Replies are listed 'Best First'.
Re: Sort textfile
by eric256 (Parson) on Apr 13, 2004 at 20:45 UTC

    Sort returns the array sorted, it does not sort it in place. So just move your sort into the foreach

    foreach my $line(sort @resultArray){

    Now it uses the sorted array returned from the sort. Alternately you could modify your sort line to save that back into the orignal array.

    @resultArray = sort { $a<=>$b } @resultArray;

    Could anyone comment as to why sort doesn't do an inplace sort when called in void context?


    ___________
    Eric Hodges

      What do you expect this would do?

      sort { $a cmp $b } qw( one two three );

      That's not intractable, but it's one of a few questions to ponder for this feature.

Re: Sort textfile
by Plankton (Vicar) on Apr 13, 2004 at 20:34 UTC
    Why not just use sort?
    $ cat junk dec.x.bc.00041669 1067503 1067501 dec.x.bc.00019580 1067503 1067501 dec.x.bc.00016113 1067503 1067501 $ sort junk dec.x.bc.00016113 1067503 1067501 dec.x.bc.00019580 1067503 1067501 dec.x.bc.00041669 1067503 1067501

    Plankton: 1% Evil, 99% Hot Gas.
Re: Sort textfile
by Sandy (Curate) on Apr 13, 2004 at 20:49 UTC
    You are using sort incorrectly.

    sort returns the sorted array, it does not sort the array.

    See example below:

    perl -e '@a=(1,5,3);@b=sort @a; print "@a\n"; print "@b\n";'
    with output
    1 5 3 1 3 5
      Guys, It's not sorting
        You have two problems in your code.

        (1) sort @array
        does not sort the array, it returns the sorted array

        (2) sort {$a <=> $b} @array
        sorts numbers, not strings.

        To fix this, use default sorting of sort (or explicitly compare strings) and assign it to a new array.

        @sorted = sort @array;
        or
        @sorted = sort {$a cmp $b} @array;

        Sandy

Re: Sort textfile
by Anonymous Monk on Apr 13, 2004 at 23:19 UTC
    print sort { $a cmp $b } <DATA>; __DATA__ dec.x.bc.00041669 1067503 1067501 dec.x.bc.00019580 1067503 1067501 dec.x.bc.00016113 1067503 1067501
Re: Sort textfile
by blue_cowdawg (Monsignor) on Apr 14, 2004 at 01:40 UTC

        Dear Monks I have a textfile, and I want to sort it based on the value of the first set (each line have three sets separate by a space)

    and in the spirit of TIMTOWTDI:

    #!/usr/bin/perl -w ########################################################### use strict; use Tie::File; my @rray=(); tie @rray,"Tie::File","myfile" or die $!; print join "\n",sort @rray; print "\n"; untie @rray;
    yields for an output:
    dec.x.bc.00016113 1067503 1067501 dec.x.bc.00019580 1067503 1067501 dec.x.bc.00041669 1067503 1067501