Thread: Sort String

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Sort String


    I am attempting to modify a perl script written by someone else.

    The script telnets into a router and puts the terminal output into a string called $output. The $output looks similar to this:

    001e.46ad.0cb8 C7/0/0/U1 927 39159 375 0 !59443 60381 Aug 23 12:57:24
    0023.a27b.6b30 C7/0/0/U1 3749 9714 364 0 !60958 64710 Aug 23 12:56:06
    1859.3353.7024 C7/0/0/U1 1829 56155 871 0 !58196 60070 Aug 23 12:57:27
    0023.a278.5865 C7/0/0/U3 7448 57683 443 0 51477 58947 Aug 23 12:00:15
    001a.de7a.3010 C7/0/0/U6 1 27012 94 0 !62146 62152 Aug 23 12:57:14
    001e.46ad.0e06 C7/0/0/U1 3851 18004 568 0 60128 63984 Aug 23 12:55:22

    I need to insert some code to sort this output numerically by the 8th column, and then feed this string back into the script so that the rest of the script doesn't break, and continues to work without knowing the original string was modified. So, after the code to sort the $output string, it should look like the following (8th column numerically sorted):

    0023.a27b.6b30 C7/0/0/U1 3749 9714 364 0 !60958 64710 Aug 23 12:56:06
    001e.46ad.0e06 C7/0/0/U1 3851 18004 568 0 60128 63984 Aug 23 12:55:22
    001a.de7a.3010 C7/0/0/U6 1 27012 94 0 !62146 62152 Aug 23 12:57:14
    001e.46ad.0cb8 C7/0/0/U1 927 39159 375 0 !59443 60381 Aug 23 12:57:24
    1859.3353.7024 C7/0/0/U1 1829 56155 871 0 !58196 60070 Aug 23 12:57:27
    0023.a278.5865 C7/0/0/U3 7448 57683 443 0 51477 58947 Aug 23 12:00:15

    I'm not sure about numerically sorting a string based on columns. Can you suggest some code that can do this? Thanks.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    843
    Rep Power
    496
    Hi,

    I think you will have to:
    - split the output into an array of lines
    - split each line into an array (so you'll have to manage an array of arrays)
    - sort numerically the first array according to the 8th element of the second
    - merge everything back together.

    Only the sort is a bit complicated, because you'll need to write your own compare routine (a function or a block) to pass it to the sort function. This could look like something like that:

    sort { $a[7] - $b[7] } @array ...

    The point is that the sort function needs a piece of code that will return a positive number is $a > $b, a negative number if $b > $a, and 0 if they are equal.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0
    figured it out
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0
    answer below
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0
    Found some code to do this using the Schwartzian Transform

    $output = join "",
    map { $_->[0]."\n" }
    sort { $b->[1] <=> $a->[1] }
    map { [$_, (split)[7]] }
    split /\n/, $output;

    Now it sorts the 8th column descending.

IMN logo majestic logo threadwatch logo seochat tools logo