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

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

Replies are listed 'Best First'.
Re^3: Sorting on different fields
by Aristotle (Chancellor) on Jul 23, 2002 at 14:04 UTC
    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

        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.

      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

Re: Re: Re: Sorting on different fields
by mkmcconn (Chaplain) on Jul 23, 2002 at 14:38 UTC

    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