in reply to File Sorting

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.

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

    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) ;
    }

Re: Re: File Sorting
by rchou2 (Novice) on Jul 19, 2002 at 18:37 UTC
    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?