Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^3: Sorting on different fields

by Aristotle (Chancellor)
on Jul 23, 2002 at 14:04 UTC ( [id://184418]=note: print w/replies, xml ) Need Help??


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

The second line creates an array for each element of the input list; its elements are the entire input string ($_) in the first element, and the first three words of the input (pulled out using the parens in the pattern match) in the next elements. The first line then later goes and pulls that first element containing the entire string back out of these arrays. Check out perldoc -f map to understand how it works.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re^3: Sorting on different fields
by rchou2 (Novice) on Jul 23, 2002 at 14:22 UTC
    right now i'm trying to use this, but it doesn't seem to work..
    my @data while (my $line = <TEMPFILE>) { my @elements = $line; push @data, \@elements; # need to store as ref } @data = sort { $$a[2] cmp $$b[2] or $$a[1] <=> $$b[1] } @data or die + "can't sort"; foreach my $item (@data) { print TEMPFILE @$item; print TEMPFILE "\n"; }

    Edit by tye

      Sidenote: can you please please do us a favour and read Writeup Formatting Tips. Your posts are just about unreadable as is. Specifically, at least learn about the <code> tag.

      Remove the or die "Can't sort". The or forces scalar context on its left operand, which is not what you want.

      Makeshifts last the longest.

Re: Re^3: Sorting on different fields
by rchou2 (Novice) on Jul 23, 2002 at 14:39 UTC
    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....
      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.

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://184418]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 08:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found