in reply to Sort based on a delimited field in array elements without affecting original array elements

Umm, in this case, you really don't have to do anything special. This worked fine for me:

my @array = <DATA>; @array = sort { $a cmp $b } @array; print @array; __DATA__ 100!100!key!date!2.. 100!100!key!store! 110!110!P\d\d\d_.*\.\d{6,}\.EMRemr 120!125! 1!yeada 130!132! 140!133! 150!134! 500!blah 160!135!arbor_ama 503!blah 170!136! 505!blah 180!137! 190!138!
It produces:
1!yeada 100!100!key!date!2.. 100!100!key!store! 110!110!P\d\d\d_.*\.\d{6,}\.EMRemr 120!125! 130!132! 140!133! 150!134! 160!135!arbor_ama 170!136! 180!137! 190!138! 500!blah 503!blah 505!blah
I don't know if it's just the sample data you're using, but from your output, you really want to sort on the whole line, lexicographically.

HTH

  • Comment on Re: Sort based on a delimited field in array elements without affecting original array elements
  • Select or Download Code