From: Gregory Brown Date: 2007-02-07T00:52:01+09:00 Subject: Re: Parse csv similar file On 2/6/07, Rebhan, Gilbert wrote: > > Hi, > > > > i have a txtfile with a format like that = > > AP850KP;INCLIB;E023889;AP013;240107;0730 > AP850SD$;INCLIB;E052337;AP013;240107;0730 > AP850SDA;INCLIB;E050441;AP013;240107;0730 > AP850SDI;INCLIB;E023889;AP013;240107;0730 > AP850SDO;INCLIB;E052337;AP013;240107;0730 > AP850SDS;INCLIB;E050441;AP013;240107;0730 > ... > > i want to get a collection for every E followed by digits, > so with the example above, i want to get = > > collections: > E023889 > E052337 > E050441 > ... > > each collection should contain datasets with the rest of the line, so > f.e. > E023889 would have = > > [AP850KP;INCLIB;AP013;240107;0730,AP850SDI;AP013;240107;0730] > > questions= > what kind of collection is the best ? is an array sufficient ? Just for fun, here's a Ruport example: require "rubygems" require "ruport" DATA = <<-EOS AP850KP;INCLIB;E023889;AP013;240107;0730 AP850SD$;INCLIB;E052337;AP013;240107;0730 AP850SDA;INCLIB;E050441;AP013;240107;0730 AP850SDI;INCLIB;E023889;AP013;240107;0730 AP850SDO;INCLIB;E052337;AP013;240107;0730 AP850SDS;INCLIB;E050441;AP013;240107;0730 EOS table = Ruport::Data::Table.parse(DATA, :has_names => false, :csv_options=>{:col_sep=>";"}) table.column_names = %w[c1 c2 c3 c4 c5 c6] # BUG! you shouldn't need colnames e = table.column(2).uniq e.each { |x| table.create_group(x) { |r| r[2].eql?(x) } } groups = table.groups >> groups.attributes >> ["E023889", "E052337", "E050441"] >> groups["E023889"].map { |r| r[0] } >> ["AP850KP", "AP850SDI"] >> groups.each { |t| p t[0].c1 } "AP850KP" "AP850SD$" "AP850SDA" =============== note that in making this example, I found a small bug in Ruport's grouping support which I will fix :)