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

Re: Sorting on different fields

by mkmcconn (Chaplain)
on Jul 23, 2002 at 03:07 UTC ( [id://184313]=note: print w/replies, xml ) Need Help??


in reply to Sorting on different fields

rchou2, what are you looking for that wasn't answered for you here?
mkmcconn

Replies are listed 'Best First'.
Re: Re: Sorting on different fields
by rchou2 (Novice) on Jul 23, 2002 at 13:49 UTC
    sorry, i'm new to perl, so if some of the questions are obvious, i apologize.....what's the purpose of these 2 lines?
    map {$_->[0]} map { $_ ,/(\w+)\s+(\w+)\s+(\w+)/} <DATA>

    Edit by tye

      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.

        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

        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....

      There is nothing obvious about what's going on there, rchou2. Although it solves your problem (I think), it's not a very good answer because it's hard to explain and to understand. That's nothing in my favor - I can't immediately think of the way I would have done it before learning this trick.

      Read more about the Schwartzian Transform to understand it. Especially, try to understand why it changes everything to remove the square brackets as you did in your cut and paste (you need them, to create the temporary arrays that are being used for sorting).
      mkmcconn

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-16 22:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found