Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: Sorting on different fields

by mkmcconn (Chaplain)
on Jul 23, 2002 at 03:07 UTC ( #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?

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 it...thanks a 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).

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2023-02-08 23:53 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (44 votes). Check out past polls.