in reply to Re^3: Sorting on different fields
in thread Sorting on different fields

ok....got it...thanks a lot.....one last question...if i want to add more columns into my file, how would i change the sort routine? specifically, the map function....

Replies are listed 'Best First'.
Re^5: Sorting on different fields
by Aristotle (Chancellor) on Jul 23, 2002 at 14:44 UTC
    Hmm. I don't see any map in the snippet you posted. Actually, unless it got skipped in the copy paste, it doesn't seem like you're separating your input into columns at all.

    Makeshifts last the longest.

Re: Re: Re^3: Sorting on different fields
by mkmcconn (Chaplain) on Jul 23, 2002 at 17:17 UTC

    You don't need the map function, rchou2. You took care of everything by using the for() loop. However, you did not split the data into fields.

    Here's one way to do what you are asking, that borrows most of the syntax preferences from your example:

    #!/usr/bin/perl -w use strict; rename 'tempfile.txt', 'tempfile.txt~'; # save a backup open TEMPFILE, "<tempfile.txt~" or die $!; my @data; my @newcolumn = qw(d e f); my $incr = 0; for my $line (<TEMPFILE>) { my @elements = split(/\s+/g, $line); # split the line on all w +hitespace push @elements, $newcolumn[$incr++]; # add a new column push @data, \@elements; # need to store as ref } close TEMPFILE; @data = sort { $$a[2] cmp $$b[2] || $$b[1] <=> $$a[1] } @data # or d +ie "can't sort"; $, = " " ; # separate output fields by a space open OUTFILE, ">tempfile.txt" or die $!; foreach my $item (@data){ print OUTFILE "@$item\n"; } close OUTFILE; __END__

    mkmcconn