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

Hi, I have a script that prints outputs into a temp file that looks like this: name x y z abc 1 4 3 xyz 2 8 5 I want to sort the temp file and print the values onto another file "values", with descending order based on z. so output would look like: name x y z xyz 2 8 5 abc 1 4 3 Thanks

Replies are listed 'Best First'.
Re: File Sorting
by bronto (Priest) on Jul 19, 2002 at 16:06 UTC

    Could you please read the Site How To and post at least one decently formatted question please? Thanks!

    Frankly speaking, your descending order based on z seems a quite random order: I really can't see which sorting algorithm you used to obtain x y z xyz 2 8 5 abc 1 4 3 from x y z abc 1 4 3 xyz 2 8 5

    This quick-and-dirty (and ***untested***) snippet does a standard reverse sort: you can take it and adapt it for your needs.

    use strict ; use warnings ; my $tempfile = "temp_file_name.txt" ; my $outfile = "/path/to/values" ; open IN, $tempfile or die $! ; open OUT, $outfile or die $! ; while (<IN>) { chomp ; my ($name,@values) = split ; print OUT join(" ",$name,reverse sort @values),"\n" ; } close OUT ; close IN ;

    Ciao!
    --bronto

    # Another Perl edition of a song:
    # The End, by The Beatles
    END {
      $you->take($love) eq $you->made($love) ;
    }

Re: File Sorting
by broquaint (Abbot) on Jul 19, 2002 at 16:25 UTC
    Firstly parse the temp file
    open(my $fh, "tempfile") or die("ack - $!"); chomp( my $header = <$fh> ); my(undef, @headers) = split ' ', $header; my(%cols) = do { my $i = 0; map { $_ => $i++ } @headers; }; my %info; while(<$fh>) { chomp; next if m< ^ \s* \z >x; my($name, @rows) = split; push @{ $info{$name} }, @rows; } close $fh;
    Then sort on the 'z' column
    open(my $fh, ">", "output") or die("ack - $!"); print $fh $header; print $fh "$_ @{ $info{$_} }\n" for reverse sort { $info{$a}->[ $cols{z} ] <=> $info{$b}->[ $cols{z} ] } keys %info; close $fh;
    Of course a much better idea would be to use one of the CSV type modules such as tilly's Text::xSV or Text::CSV_XS.
    HTH

    _________
    broquaint

    update: changed header regex => split, as I had a bad meme about multiple matches pushing onto the return list, unfortunately not e.g

    my(@list) = "foo bar baz" =~ m< (?: (\w+) \s* )+ >x; print "@list"; __ouput__ baz # not - foo bar baz

    update 2: posting on a Friday != a good idea

    my(@list) = "foo bar baz" =~ m< (\w+) [ ]? >xg; print "@list"; __output__ foo bar baz
    the previous example not working presumably because it was missing the g modifier, and the subsequent captures over-rode the previous one.

      WOW!

      package Bronto ; use Parser::Rchou2 ; our $broquaint = Parser::Rchou2->new(node_id => 183282) ;

      You will get a ++ from me tomorrow, one + for the script and one for the translation of rchou2's post ;-)

      Ciao!
      --bronto

      # Another Perl edition of a song:
      # The End, by The Beatles
      END {
        $you->take($love) eq $you->made($love) ;
      }

      i run into a prolem if the file has three columns, the first is a name (string), the second and third are numerical, then how would i sort it based on the third column in descending value?