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|

.



Relevant Pages

  • Re: sorting data - hash vs. list
    ... >> My dilemma is that I have to sort a flat file on a certain field. ... >puts DATA.sort_by{|x| ... I have to check our Ruby, ...
    (comp.lang.perl.misc)
  • Ruby script to Module/Class refactor
    ... I have written my first live ruby script that actually performs useful ... # and append .csv extension to each. ... puts FileTest.readable?? ...
    (comp.lang.ruby)
  • Re: lowering rubys reach
    ... Don't get me wrong it should make it a lot easier to write extensions for ruby - especially those that are merely for speed gains. ... What I was think of was the introduction of language profiles which would trigger certain language features. ... would complain about variable a not being declared and would need to be change to include a forward declaration, ...
    (comp.lang.ruby)
  • Re: Suggestion: swap name of "puts" and "print" and rename "puts" to "put_s"
    ... character and "puts" doesn't. ... Because that's how it is in the C language: ... Either way, just make it consistent. ... One of the benefits I have found from using Ruby is that it actively ...
    (comp.lang.ruby)
  • Re: Mind giving a little help?
    ... but Ben showed you two techniques which will work. ... puts "Is this URL OK? ... going to be running this program via command promt, I'm not using Ruby ... People are much more likely to answer a short concise question like 'how do I read a file?' ...
    (comp.lang.ruby)