in reply to Sort options

($a) cmp ($b) is comparing the full strings, not just the alphabetics, so perl never gets to the second comparison (which should be or'ed, BTW). You need something like

my @split_list = qw{erez[11] dana[22] dana[0] erez[10] erez[1] erez[0] + dana[10]}; my @new_list = sort { ($a =~ /(\w+)/)[0] cmp ($b =~ /(\w+)/)[0] || ($a =~ /(\d+)/)[0] <=> ($b =~ /(\d+)/)[0] } @split_list; print "$_\n" for @new_list; __END__ dana[0] dana[10] dana[22] erez[0] erez[1] erez[10] erez[11]


Unless I state otherwise, all my code runs with strict and warnings

Replies are listed 'Best First'.
Re^2: Sort options
by Perlbotics (Archbishop) on Aug 03, 2008 at 17:09 UTC
    If the OPs list is allowed to have elements like i.e. dana1[23], then it would make sense to let the opening square bracket remain part of the regexp like it was before: (... =~ /\[(\d+)/)[0].
Re^2: Sort options
by erez_ez (Acolyte) on Aug 03, 2008 at 15:40 UTC
    Great! thanks. But what if I get my list from a file? How do I use the 'qw' function? I tried operating it on an array but it doesnt work...
    my @split_list = qw{@list};
    Is this correct?
      Did you first look for qw{} in the documentation/perl book/man pages before asking here?

      qw is a way to specify literal lists. @a= qw{ a b c } is equivalent to  @a= ('a','b','c');. If you already have an array, you don't need qw.