#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    0
    Rep Power
    0
    Hi,

    I'm hoping some one can help me with this .. ^please^
    I am trying to sort a simple file and I'd like to know how to sort by any field I specify. I've looked everywhere and I'm really having a difficult time understanding it. A simple lamens explanation would be so much appreciated

    My example file:
    Name|age|Birthday

    Beverly|21|02/01/79
    Don|32|23/08/68
    Grant|20|10/10/80

    etc..

    I know how to do the generic sorting of the names. I also know how to sort numerically ( <=> ) I am just stuck on how to sort let's say by the age, or even the birthday. It works if I sort and print out what I want to sort, but then when I print out the name + age + birthday it resorts to being sorted by the names again?

    I've tried the Schwartzian Transform, and I get it to work sort of, but I really don't understand what it's doing? I've read everything I possibly could on it, but most examples of this are just the code with hard to understand literature.

    @lines = map { $_->[0] }
    sort { $a->[1] cmp $b->[1] }
    map { [$_,(split /|/)[0] ] } @lines;

    I would like the file to come out reading something like this,

    by age:
    1. Grant - 20 - 10/10/80
    2. Beverly - 21 - 02/01/79
    3. Don - 32 - 23/08/68

    or by birthdate:
    1. Beverly - 21 - 02/01/79
    2. Grant - 20 - 10/10/80
    3. Don - 32 - 23/08/68

    The names are easy since it I am sorting from just the file

    Please don't redirect me to http://www.perl.com/CPAN-local/modules/by-module/Sort/ 'cause I don't really understand that either

    Please help. I am very grateful to any explanation you can offer.

    Thank-You
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    0
    Rep Power
    0
    Thanks to anyone who gave this post a thought or two I found what I needed and all is well now

    For anyone interested, this tutorial, helped me break it down. http://www.5sigma.com/perl/schwtr.html

    thanks again!!

IMN logo majestic logo threadwatch logo seochat tools logo