Re: sorting data - hash vs. list
- From: "William James" <w_a_x_man@xxxxxxxxx>
- Date: 12 Sep 2005 02:04:43 -0700
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|
.
- Follow-Ups:
- Re: sorting data - hash vs. list
- From: Fred@xxxxxxxx
- Re: sorting data - hash vs. list
- References:
- sorting data - hash vs. list
- From: Fred@xxxxxxxx
- sorting data - hash vs. list
- Prev by Date: Re: How to redirect stderr to C lang function
- Next by Date: Re: Space (\s) count problem
- Previous by thread: Re: sorting data - hash vs. list
- Next by thread: Re: sorting data - hash vs. list
- Index(es):
Relevant Pages
|