perl sorting

Discussion in 'Perl Misc' started by Deepu, Feb 11, 2011.

  1. Deepu

    Deepu Guest

    Hi All,

    I need some help in sorting a file numerically based on the numbers in
    a specified column.

    File contents:

    [1000.123]:- 10.234 -- tempa.tempc -> tempd.tempe
    [893.234]:- 8.89 -- tempe.tempw -> tempe.tempd
    [893.234]:- 15.75 -- tempc.tempg -> tempt.tempr

    I need to get an output in ascending order of the numbers in column2.

    Output:

    [893.234]:- 8.89 -- tempe.tempw -> tempe.tempd
    [1000.123]:- 10.234 -- tempa.tempc -> tempd.tempe
    [893.234]:- 15.75 -- tempc.tempg -> tempt.tempr

    Thanks..
    Deepu, Feb 11, 2011
    #1
    1. Advertising

  2. Deepu

    Keith Keller Guest

    On 2011-02-11, Deepu <> wrote:
    >
    > I need some help in sorting a file numerically based on the numbers in
    > a specified column.
    >
    > File contents:
    >
    > [1000.123]:- 10.234 -- tempa.tempc -> tempd.tempe
    > [893.234]:- 8.89 -- tempe.tempw -> tempe.tempd
    > [893.234]:- 15.75 -- tempc.tempg -> tempt.tempr
    >
    > I need to get an output in ascending order of the numbers in column2.
    >
    > Output:
    >
    > [893.234]:- 8.89 -- tempe.tempw -> tempe.tempd
    > [1000.123]:- 10.234 -- tempa.tempc -> tempd.tempe
    > [893.234]:- 15.75 -- tempc.tempg -> tempt.tempr


    You should post a short but complete snippet of your Perl code that is
    giving you difficulties.

    --keith

    --
    -francisco.ca.us
    (try just my userid to email me)
    AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
    see X- headers for PGP signature information
    Keith Keller, Feb 11, 2011
    #2
    1. Advertising

  3. Deepu

    Jim Gibson Guest

    In article
    <>,
    Deepu <> wrote:

    > Hi All,
    >
    > I need some help in sorting a file numerically based on the numbers in
    > a specified column.
    >
    > File contents:
    >
    > [1000.123]:- 10.234 -- tempa.tempc -> tempd.tempe
    > [893.234]:- 8.89 -- tempe.tempw -> tempe.tempd
    > [893.234]:- 15.75 -- tempc.tempg -> tempt.tempr
    >
    > I need to get an output in ascending order of the numbers in column2.
    >
    > Output:
    >
    > [893.234]:- 8.89 -- tempe.tempw -> tempe.tempd
    > [1000.123]:- 10.234 -- tempa.tempc -> tempd.tempe
    > [893.234]:- 15.75 -- tempc.tempg -> tempt.tempr


    Have you read the following?:

    perldoc -f sort
    perldoc -q sort

    This is one sort function that should work:

    @sorted =sort { (split(/ /,$a))[1] <=> (split(/ /,$b))[1] } @unsorted;

    Making that more efficient is left as an exercise (the above performs
    the split function more times than is necessary). See the perldoc
    references for how.

    --
    Jim Gibson
    Jim Gibson, Feb 11, 2011
    #3
  4. Στις 12/2/2011 12:15 πμ, ο/η Deepu έγÏαψε:
    > [1000.123]:- 10.234 -- tempa.tempc -> tempd.tempe
    > [893.234]:- 8.89 -- tempe.tempw -> tempe.tempd
    > [893.234]:- 15.75 -- tempc.tempg -> tempt.tempr



    my @i;
    while(<DATA>) {/:- (\S+) --/ && push @i, [$1,$_]}
    foreach (sort{$a->[0] <=> $b->[0]} @i) { print $_->[1] }

    __DATA__
    [1000.123]:- 10.234 -- tempa.tempc -> tempd.tempe
    [893.234]:- 8.89 -- tempe.tempw -> tempe.tempd
    [893.234]:- 15.75 -- tempc.tempg -> tempt.tempr
    George Mpouras, Feb 11, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,794
  2. Replies:
    2
    Views:
    1,427
    James Kanze
    Jul 6, 2010
  3. Jason
    Replies:
    0
    Views:
    384
    Jason
    Oct 4, 2006
  4. Tom Kirchner

    sorting by multiple criterias (sub-sorting)

    Tom Kirchner, Oct 11, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    474
    Michael Budash
    Oct 11, 2003
  5. Íéêüëáïò Êïýñáò

    Sorting a set works, sorting a dictionary fails ?

    Íéêüëáïò Êïýñáò, Jun 10, 2013, in forum: Python
    Replies:
    12
    Views:
    157
    Ulrich Eckhardt
    Jun 10, 2013
Loading...

Share This Page