Re: sorting data - hash vs. list




Fred@xxxxxxxx wrote:

> My dilemma is that I have to sort a flat file on a certain field.
> Now in Unix you can `sort -d"|" -f13` and viola sort the 13th field of a well-mannered data file. BUT in perl so far, I have had no luck. Allow me first to introduce my/our/the data record.
>
> |C|170|901|2|0000000000008|4|000008|5|09/05/05|6|BART'S HVYDTY PROPHYLACTIC|14|3.99|15|00006|

In Ruby:
--------------------------------------------------
puts DATA.sort_by{|x|
x =~ %r:(\d\d)/(\d\d)/(\d\d): ; [$3,$1,$2] }

__END__
|C|170|901|2|0000000000008|4|000008|5|08/30/05|6|
|C|170|901|2|0000000000003|4|000008|5|05/06/06|6|
|C|170|901|2|0000000000001|4|000008|5|09/05/06|6|
|C|170|901|2|0000000000028|4|000008|5|09/06/05|6|
--------------------------------------------------

Output:

|C|170|901|2|0000000000008|4|000008|5|08/30/05|6|
|C|170|901|2|0000000000028|4|000008|5|09/06/05|6|
|C|170|901|2|0000000000003|4|000008|5|05/06/06|6|
|C|170|901|2|0000000000001|4|000008|5|09/05/06|6|

.