sorting a file with records

Discussion in 'Perl' started by mike, Nov 13, 2003.

  1. mike

    mike Guest

    hi

    i have a sample file like this (field separator is a space):


    orion 13-11-2003 mike
    jupiter 11-11-2003 susan
    venus 12-11-2002 alvin
    neptune 10-11-2002 sharon

    i just want to sort this file on the first column by string so
    eventaully the output is:

    jupiter 11-11-2003 susan
    neptune 10-11-2002 sharon
    venus 12-11-2002 alvin
    orion 13-11-2003 mike

    how can i do that just with the sort function from perl??
    thanks very much for any help
    mike, Nov 13, 2003
    #1
    1. Advertising

  2. mike

    Jim Gibson Guest

    In article <>, mike
    <> wrote:

    > hi
    >
    > i have a sample file like this (field separator is a space):

    [SNIP]
    >
    > i just want to sort this file on the first column by string so
    > eventaully the output is:
    >

    [SNIP]
    >
    > how can i do that just with the sort function from perl??
    > thanks very much for any help


    Put the data to sort in an array. If you have the data in a file
    (data.txt for example), you can do this with:

    open(IN,"data.txt") or die("Can't open file");
    @array = <IN>;

    You can then create a sorted array with:

    @sorted_array = sort @array;

    FYI: this newsgroup is defunct. Try comp.lang.perl.misc in the future
    for better responses.
    Jim Gibson, Nov 14, 2003
    #2
    1. Advertising

  3. mike

    GIMME Guest

    Why not use the UNIX sort command ?

    sort -1 filename.txt


    (mike) wrote in message news:<>...
    > hi
    >
    > i have a sample file like this (field separator is a space):
    >
    >
    > orion 13-11-2003 mike
    > jupiter 11-11-2003 susan
    > venus 12-11-2002 alvin
    > neptune 10-11-2002 sharon
    >
    > i just want to sort this file on the first column by string so
    > eventaully the output is:
    >
    > jupiter 11-11-2003 susan
    > neptune 10-11-2002 sharon
    > venus 12-11-2002 alvin
    > orion 13-11-2003 mike
    >
    > how can i do that just with the sort function from perl??
    > thanks very much for any help
    GIMME, Nov 18, 2003
    #3
    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. mike
    Replies:
    1
    Views:
    436
    Gunnar Hjalmarsson
    Nov 13, 2003
  2. Haydn Williams

    Sorting datalist records horizontally

    Haydn Williams, Sep 9, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    580
    Frank Mamone
    Sep 9, 2004
  3. Luke Airig
    Replies:
    0
    Views:
    765
    Luke Airig
    Dec 31, 2003
  4. Dan

    Delete records or update records

    Dan, May 10, 2004, in forum: ASP General
    Replies:
    1
    Views:
    449
    Ray at
    May 10, 2004
  5. Replies:
    3
    Views:
    638
    Anthony Jones
    Nov 2, 2006
Loading...

Share This Page