Professional Web Applications Themes

How to Sort on a substr - PERL Beginners

Hi, I need to do a sort on a couple of column ranges. I want to be able to do a primary sort, on say, columns 21-25 and then a secondary sort on columns 40-49. Any ideas on how to approach this? TIA /j __________________________________ Do you Yahoo!? Yahoo! Mail - You care about security. So do we. http://promotions./new_mail...

  1. #1

    Default How to Sort on a substr

    Hi,

    I need to do a sort on a couple of column ranges.

    I want to be able to do a primary sort, on say, columns 21-25 and
    then a secondary sort on columns 40-49.

    Any ideas on how to approach this?

    TIA


    /j




    __________________________________
    Do you Yahoo!?
    Yahoo! Mail - You care about security. So do we.
    http://promotions./new_mail
    Jeff Guest

  2. #2

    Default Re: How to Sort on a substr

    Jeff Westman wrote: 

    Yes.

    1. Check out the docs for the sort() function.

    2. Check the Perl FAQ:

    perldoc -q "How do I sort an array by (anything)?"

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Guest

  3. #3

    Default Re: How to Sort on a substr

    Jeff Westman wrote: 

    Can you get away with concatenating the 2 substrings? That would cut
    down your sort to pass. Either way you'll have to define a sort
    function I think.


    Rob Guest

  4. #4

    Default Re: How to Sort on a substr

    On Thursday 15 July 2004 13:54, Jeff Westman wrote: 

    Hello,
     

    This should work:

    my sorted = map substr( $_, 15 ),
    sort
    map substr( $_, 20, 5 ) . substr( $_, 39, 10 ) . $_,
    unsorted;


    John
    --
    use Perl;
    program
    fulfillment

    John Guest

  5. #5

    Default RE: How to Sort on a substr

    Jeff Westman wrote: 

    Assuming you're numbering columns from 1:

    rows = sort { substr($a, 20, 5) cmp substr($b, 20, 5)
    || substr($a, 49, 10) cmp substr($b, 49, 10) } rows;

    If you're numbering columns from 0, adjust the offsets above.

    The general concept for multi key sorting is to combine the comparisons with
    a logical "or" operation.

    John Krahn gave you a sort of modified "Schwartzian Transform" (did I spell
    that right?) approach which is faster if you have a large number of rows to
    process, but a bit less clear...
    Bob Guest

Similar Threads

  1. using substr... is this efficient
    By Larry in forum PERL Beginners
    Replies: 4
    Last Post: February 20th, 10:40 PM
  2. substr parsing mask
    By perl@swanmail.com in forum PERL Beginners
    Replies: 4
    Last Post: September 29th, 03:11 PM
  3. Replies: 1
    Last Post: September 9th, 07:16 AM
  4. [PHP] substr ?
    By John Taylor-Johnston in forum PHP Development
    Replies: 0
    Last Post: August 17th, 05:49 AM
  5. InStr, substr, Mid ???
    By J. Muenchbourg in forum ASP
    Replies: 1
    Last Post: August 13th, 12:18 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139