in reply to Re: Comparison of alphanumeric strings
in thread Comparison of alphanumeric strings

I want to order them in ascending order depending on the numbers first, then strings and then alphanumeric
  • Comment on Re^2: Comparison of alphanumeric strings

Replies are listed 'Best First'.
Re^3: Comparison of alphanumeric strings
by GrandFather (Saint) on Sep 17, 2009 at 11:00 UTC

    In that case you really need two sort criteria - type of string and actual string. Consider:

    use warnings; use strict; my $column=2; my @sort_col; my @lines; while(<DATA>) { my @F=split(/\|/,$_); push @sort_col, $F[$column]; push @lines, "$_"; } @sort_col = map {[$_ => /^\d+$/ ? 0 : /^\D+$/ ? 1 : 2]} @sort_col; my @sortedOrder = sort {$sort_col[$a][1] cmp $sort_col[$b][1] or $sort_col[$a][0] cm +p $sort_col[$b][0]} 0 .. $#sort_col; print @lines[@sortedOrder]; __DATA__ D|0|95465tan|2|4|4|14028.77|0.00|BF2-002|22/09/2009|07/01/2009|06/09/2 +009|45544564| D|0|954564545|1|4|abar_BF4|4|3913.17|0.00|accabar_BF4-003|22/09/2009|0 +7/02/2009|06/09/2009|10254754| D|0|a454654|1|4|ar_BF4|4|3913.17|0.00|acctrabar_BF4-003|22/09/2009|07/ +02/2009|06/09/2009|11122323| D|0|Chking|1|4|arabar_BF4|4|3913.17|0.00|acarabar_BF4-003|22/09/2009|0 +7/02/2009|06/09/2009|11122323|

    Prints:

    D|0|954564545|1|4|abar_BF4|4|3913.17|0.00|accabar_BF4-003|22/09/2009|0 +7/02/2009|06/09/2009|10254754| D|0|Chking|1|4|arabar_BF4|4|3913.17|0.00|acarabar_BF4-003|22/09/2009|0 +7/02/2009|06/09/2009|11122323| D|0|95465tan|2|4|4|14028.77|0.00|BF2-002|22/09/2009|07/01/2009|06/09/2 +009|45544564| D|0|a454654|1|4|ar_BF4|4|3913.17|0.00|acctrabar_BF4-003|22/09/2009|07/ +02/2009|06/09/2009|11122323|

    True laziness is hard work
      Grandfather, thanks for the reply. but one problem is that if the input is like given below it inserts a space between the lines:
      D|06/09/2009|1454| D|06/09/2009|2123cus| D|06/09/2009|4123| D|06/09/2009|aus| D|06/09/2009|aaus12321|
      :(

        Not for me with the sample code I posted. Either the data you posted doesn't match the data you saw the issue with, or your actual code performs processing on the data that my sample doesn't and that processing is introducing the problem. Could it be that the number of columns is important for your extra processing?

        It may help to chomp each input line then append a newline to each line when you print.


        True laziness is hard work
      Grandfather, that worked perfectly thanks a lottt