Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

problem sorting by non-consecutive fields

by Anonymous Monk
on Apr 07, 2004 at 20:26 UTC ( [id://343420]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks , this stupid sorting problem is driving me crazy : I have the following input :
7 modify ldfapg pub abc.h 8 modify bfxml dtd/src newfiles.ksh 9 delete bfxml dtd/src newfiles.ksh 10 modify bfxml dtd/src newfiles.ksh
I am trying search first by the third field and then by the first one so I want my output to look like this :
8 modify bfxml dtd/src newfiles.ksh 9 delete bfxml dtd/src newfiles.ksh 10 modify bfxml dtd/src newfiles.ksh 7 modify ldfapg pub abc.h
the problem is , I keep getting the following :
7 modify ldfapg pub abc.h 8 modify bfxml dtd/src newfiles.ksh 9 delete bfxml dtd/src newfiles.ksh 10 modify bfxml dtd/src newfiles.ksh
using sort +2 +0 -n file the third field should be sort based on the first letter and the first based on the number . thanks

Replies are listed 'Best First'.
Re: problem sorting by non-consecutive fields
by borisz (Canon) on Apr 07, 2004 at 20:39 UTC
    #!/usr/bin/perl while (<DATA>) { push @array, [ split ' ' ]; } @array = sort { $a->[2] cmp $b->[2] or $a->[0] <=> $b->[0] } @array; for ( @array ) { print join(' ', @$_), "\n"; } __DATA__ 7 modify ldfapg pub abc.h 8 modify bfxml dtd/src newfiles.ksh 9 delete bfxml dtd/src newfiles.ksh 10 modify bfxml dtd/src newfiles.ksh
    Boris
Re: problem sorting by non-consecutive fields
by BazB (Priest) on Apr 07, 2004 at 20:37 UTC
Re: problem sorting by non-consecutive fields
by davido (Cardinal) on Apr 07, 2004 at 20:40 UTC
    One way to do it would be to use a Schwartzian Transform combined with the Logical Short Circuit 'or' operator for sort comparison fall-through.

    my @data = ( "7 modify ldfapg pub abc.h", "8 modify bfxml dtd/src newfiles.ksh", "9 delete bfxml dtd/src newfiles.ksh", "10 modify bfxml dtd/src newfiles.ksh" ); my @sorted = map { $_->[0] } sort { $a->[1][2] cmp $b->[1][2] or $a->[1][0] <=> $b->[1][0] } map { [ $_, [ split /\s+/, $_ ] ] } @data;

    UPDATE: What do you mean by "using sort +2 +0 -n file the third field should be sort based on the first letter and the first based on the number."??? Are you using Perl and Perl's sort, or are you using your operating system's sort tool?


    Dave

Re: problem sorting by non-consecutive fields
by ccn (Vicar) on Apr 07, 2004 at 20:40 UTC
    print map { ${ $_->[2] } } sort { $a->[0] cmp $b->[0] or $a->[1] <=> $b->[1] } map { [ ( split('\s+', $_, 3) )[2, 0], \$_ ] } <DATA>; __DATA__ 7 modify ldfapg pub abc.h 8 modify bfxml dtd/src newfiles.ksh 9 delete bfxml dtd/src newfiles.ksh 10 modify bfxml dtd/src newfiles.ksh
Re: problem sorting by non-consecutive fields
by graff (Chancellor) on Apr 08, 2004 at 03:55 UTC
    Others have posted appropriate perl solutions to your sorting problem, which should be helpful -- and may even point to things you can do with perl that you can't do on the command line with the unix "sort" utility.

    And folks are right -- this is a perl site, not a unix tool site (even though perl counts in many ways as a tool for unix users). Still, I can't resist pointing out that you are using the wrong options on the unix sort command line -- I think it would work as you intended if done like this:

    sort -k 3,3 -k 1,1n file
    Reading all the way through the unix man page for "sort" should explain how the "-k" option is used. (Note that column indexes for "-k" start at 1, not at 0.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2024-03-29 21:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found