Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default sort issue

    Hi there,
    I need to sort elements of of list/array by character. For instance I
    got "hdisk1 hdisk10 hdisk11 hdisk2 hdisk3 ... " and need to have
    "hdisk1 hdisk2 hdisk3 ... hdisk10 hdisk11 ..."
    I can do that using external programs like
    $ cat file_with_hdisks | sort -n -k1.6

    but I'd like to do it in perl. Does anyone have any piece of code or
    at least idea how to achieve that in as easy way as passible?

    Thanks,
    Bogdan
    Bogdan Guest

  2. Similar Questions and Discussions

    1. Sort a ds
      Hello, I need to sort a ds. I did this via a view and now I want to send the view back to the ds. How can I do that? Thanks
    2. datagrid sort issue
      Hello group, The webpage I am working with has 3 datagrids. All the 3 datagrids pretty much does the same thing expect for some minor variations....
    3. A sort of IF THEN ELSE in SQL
      Hi All I know this isn't an SQL syntax NG per say (esp MySQL), but there are far more legible gurus in here than the MySQL groups so I hope you...
    4. memory sort and disk sort
      I check the sysprofile table and find there are 700 times disk sort, I think it is lack of sort memory. I want to turn all the disk sort into the...
    5. Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB.
      Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. hi, guys i have asp...
  3. #2

    Default Re: sort issue

    X-Ftn-To: Bogdan

    [email]bogdan_czyz@hotmail.com[/email] (Bogdan) wrote:
    >Hi there,
    >I need to sort elements of of list/array by character. For instance I
    >got "hdisk1 hdisk10 hdisk11 hdisk2 hdisk3 ... " and need to have
    >"hdisk1 hdisk2 hdisk3 ... hdisk10 hdisk11 ..."
    >I can do that using external programs like
    >$ cat file_with_hdisks | sort -n -k1.6
    >
    >but I'd like to do it in perl. Does anyone have any piece of code or
    >at least idea how to achieve that in as easy way as passible?
    [url]http://www.google.com/search?q=schwartzian[/url]

    #untested
    my @arr = qw/hdisk1 hdisk10 hdisk11 hdisk2 hdisk3/;
    @arr =
    map $_->[0],
    sort {
    $a->[1] cmp $b->[1] ||
    $a->[2] <=> $b->[2]
    }
    map [ $_, /(\w+)(\d+)/ ], @arr;



    --
    Matija
    Matija Papec Guest

  4. #3

    Default Re: sort issue

    Bogdan <bogdan_czyz@hotmail.com> wrote:
    > I need to sort elements of of list/array by character. For instance I
    ^^^^^^^^^^
    > got "hdisk1 hdisk10 hdisk11 hdisk2 hdisk3 ... "

    That is not a "list/array", that is a string.

    > and need to have
    > "hdisk1 hdisk2 hdisk3 ... hdisk10 hdisk11 ..."

    There are several Frequently Asked Questions about sorting,
    have you already seen their answers?

    > I can do that using external programs like
    > $ cat file_with_hdisks | sort -n -k1.6

    Why are you stopping at the 6th character when some of your pieces
    have 7 characters? (eg. hdisk10).

    You have a Useless Use Of Cat as well:

    sort -n -k1.6 file_with_hdisks

    > but I'd like to do it in perl. Does anyone have any piece of code or
    > at least idea how to achieve that in as easy way as passible?

    ---------------------------
    #!/usr/bin/perl
    use strict;
    use warnings;

    my @parts = split /\s+/, 'hdisk1 hdisk10 hdisk11 hdisk2 hdisk3';
    print "$_\n" for @parts; # before
    print "----\n";

    @parts = sort { substr($a, 5) <=> substr($b, 5) } @parts;
    print "$_\n" for @parts; # after
    ---------------------------


    --
    Tad McClellan SGML consulting
    [email]tadmc@augustmail.com[/email] Perl programming
    Fort Worth, Texas
    Tad McClellan Guest

  5. #4

    Default Re: sort issue

    Thanks a lot for remarks and suggestions.

    Bogdan
    Bogdan Guest

Posting Permissions

  • You may not post new threads
  • You may 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