Forum: Ruby csv next and previous help

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D3851c0aae6324a6d339cdefab56f452?d=identicon&s=25 Geoff (Guest)
on 2007-04-25 22:45
(Received via mailing list)
Hey there,

I'm trying to figure out how to work with the sample data below such
that I can take each time there is a C in the third column and update
the previous D to a P.

An ideas?

IM,0000001,D,0,@FRIEDEXT01;IMAGES\00\00;0000001.TIF;2
IM,0000002, ,0,@FRIEDEXT01;IMAGES\00\00;0000002.TIF;2
IM,0000003,D,0,@FRIEDEXT01;IMAGES\00\00;0000003.TIF;2
IM,0000004, ,0,@FRIEDEXT01;IMAGES\00\00;0000004.TIF;2
IM,0000005,C,0,@FRIEDEXT01;IMAGES\00\00;0000005.TIF;2
IM,0000006,D,0,@FRIEDEXT01;IMAGES\00\00;0000006.TIF;2

This should end up like this:

IM,0000001,D,0,@FRIEDEXT01;IMAGES\00\00;0000001.TIF;2
IM,0000002, ,0,@FRIEDEXT01;IMAGES\00\00;0000002.TIF;2
IM,0000003,P,0,@FRIEDEXT01;IMAGES\00\00;0000003.TIF;2
IM,0000004, ,0,@FRIEDEXT01;IMAGES\00\00;0000004.TIF;2
IM,0000005,C,0,@FRIEDEXT01;IMAGES\00\00;0000005.TIF;2
IM,0000006,D,0,@FRIEDEXT01;IMAGES\00\00;0000006.TIF;2

Thank you,

Geoff
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-04-26 00:07
(Received via mailing list)
On Apr 25, 2007, at 3:45 PM, Geoff wrote:

> I'm trying to figure out how to work with the sample data below such
> that I can take each time there is a C in the third column and update
> the previous D to a P.
>
> An ideas?

I've always got ideas.  ;)

 >> require "rubygems"
=> false
 >> require "faster_csv"
=> true
 >> data = <<END_DATA
IM,0000001,D,0,@FRIEDEXT01;IMAGES\00\00;0000001.TIF;2
IM,0000002, ,0,@FRIEDEXT01;IMAGES\00\00;0000002.TIF;2
IM,0000003,D,0,@FRIEDEXT01;IMAGES\00\00;0000003.TIF;2
IM,0000004, ,0,@FRIEDEXT01;IMAGES\00\00;0000004.TIF;2
IM,0000005,C,0,@FRIEDEXT01;IMAGES\00\00;0000005.TIF;2
IM,0000006,D,0,@FRIEDEXT01;IMAGES\00\00;0000006.TIF;2
END_DATA
=> "IM,0000001,D,0,@FRIEDEXT01;IMAGES\000\000;0000001.TIF;2\nIM,
0000002, ,0,@FRIEDEXT01;IMAGES\000\000;0000002.TIF;2\nIM,0000003,D,
0,@FRIEDEXT01;IMAGES\000\000;0000003.TIF;2\nIM,0000004, ,
0,@FRIEDEXT01;IMAGES\000\000;0000004.TIF;2\nIM,0000005,C,
0,@FRIEDEXT01;IMAGES\000\000;0000005.TIF;2\nIM,0000006,D,
0,@FRIEDEXT01;IMAGES\000\000;0000006.TIF;2\n"
 >> parsed = FCSV.parse(data)
=> [["IM", "0000001", "D", "0", "@FRIEDEXT01;IMAGES\000
\000;0000001.TIF;2"], ["IM", "0000002", " ", "0", "@FRIEDEXT01;IMAGES
\000\000;0000002.TIF;2"], ["IM", "0000003", "D", "0",
"@FRIEDEXT01;IMAGES\000\000;0000003.TIF;2"], ["IM", "0000004", " ",
"0", "@FRIEDEXT01;IMAGES\000\000;0000004.TIF;2"], ["IM", "0000005",
"C", "0", "@FRIEDEXT01;IMAGES\000\000;0000005.TIF;2"], ["IM",
"0000006", "D", "0", "@FRIEDEXT01;IMAGES\000\000;0000006.TIF;2"]]
 >> changed = parsed.inject(Array.new) do |rows, row|
?>   if row[2] == "C" and (d_row = rows.reverse.find { |r| r[2] ==
"D" })
 >>     d_row[2] = "P"
 >>   end
 >>   rows << row
 >> end
=> [["IM", "0000001", "D", "0", "@FRIEDEXT01;IMAGES\000
\000;0000001.TIF;2"], ["IM", "0000002", " ", "0", "@FRIEDEXT01;IMAGES
\000\000;0000002.TIF;2"], ["IM", "0000003", "P", "0",
"@FRIEDEXT01;IMAGES\000\000;0000003.TIF;2"], ["IM", "0000004", " ",
"0", "@FRIEDEXT01;IMAGES\000\000;0000004.TIF;2"], ["IM", "0000005",
"C", "0", "@FRIEDEXT01;IMAGES\000\000;0000005.TIF;2"], ["IM",
"0000006", "D", "0", "@FRIEDEXT01;IMAGES\000\000;0000006.TIF;2"]]
 >> puts changed.map { |r| r.to_csv }
IM,0000001,D,0,@FRIEDEXT01;IMAGES;0000001.TIF;2
IM,0000002, ,0,@FRIEDEXT01;IMAGES;0000002.TIF;2
IM,0000003,P,0,@FRIEDEXT01;IMAGES;0000003.TIF;2
IM,0000004, ,0,@FRIEDEXT01;IMAGES;0000004.TIF;2
IM,0000005,C,0,@FRIEDEXT01;IMAGES;0000005.TIF;2
IM,0000006,D,0,@FRIEDEXT01;IMAGES;0000006.TIF;2
=> nil

Hope that helps.

James Edward Gray II
47ba67a85d1a9a0fd8f8bf8396b0cac0?d=identicon&s=25 Ganesh Gunasegaran (Guest)
on 2007-04-26 00:09
(Received via mailing list)
A Naive implementation below,

string = %q{IM,0000001,D,0,@FRIEDEXT01;IMAGES\00\00;0000001.TIF;2
IM,0000002, ,0,@FRIEDEXT01;IMAGES\00\00;0000002.TIF;2
IM,0000003,D,0,@FRIEDEXT01;IMAGES\00\00;0000003.TIF;2
IM,0000004, ,0,@FRIEDEXT01;IMAGES\00\00;0000004.TIF;2
IM,0000005,C,0,@FRIEDEXT01;IMAGES\00\00;0000005.TIF;2
IM,0000006,D,0,@FRIEDEXT01;IMAGES\00\00;0000006.TIF;2}

puts "%%%%%%%%%% BEFOR PARSING %%%%%%%%%%%%%"
puts string

last_d_line = nil
line_array = string.split("\n")
line_array.each_with_index do |line, i|
   if line.split(',')[2] == "D"
     last_d_line = i
   end
   if line.split(',')[2] == "C"
     arr = line_array[last_d_line].split(',')
     arr[2] = "P"
     line_array[last_d_line] = arr.join(',')
   end
end

puts "%%%%%%%%%%%% PARSED %%%%%%%%%%%%%%"
puts line_array


Cheers,
Ganesh Gunasegaran
D3851c0aae6324a6d339cdefab56f452?d=identicon&s=25 Geoff (Guest)
on 2007-04-26 18:01
(Received via mailing list)
James,

It helps a lot! Still getting an error though. Here is the code I used
and the error I got:

require 'rubygems'
require 'faster_csv'

parsed = FCSV.parse('C:/ruby/work/FRIEDEXT01.LFP', 'r')
changed = parsed.inject(Array.new) do |rows, row|
  if row[2] == "C" and (d_row = rows.reverse.find { |r| r[2] == "D" })
    d_row[2] = "P"
  end
  rows << row
end
puts changed.map { |r| r.to_csv }

c:/ruby/lib/ruby/gems/1.8/gems/fastercsv-1.2.0/lib/faster_csv.rb:
1320:in `merge': can't convert String into Hash (TypeError)
        from c:/ruby/lib/ruby/gems/1.8/gems/fastercsv-1.2.0/lib/
faster_csv.rb:1320:in `initialize'
        from c:/ruby/lib/ruby/gems/1.8/gems/fastercsv-1.2.0/lib/
faster_csv.rb:1184:in `new'
        from c:/ruby/lib/ruby/gems/1.8/gems/fastercsv-1.2.0/lib/
faster_csv.rb:1184:in `parse'
        from C:/ruby/work/lfp_parent.rb:4

Thanks,
Geoff

On Apr 25, 3:05 pm, James Edward Gray II <j...@grayproductions.net>
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-04-26 18:39
(Received via mailing list)
On Apr 26, 2007, at 11:00 AM, Geoff wrote:

> It helps a lot!

Good.

> Still getting an error though.

I bet we can fix that.

> Here is the code I used and the error I got:
>
> require 'rubygems'
> require 'faster_csv'
>
> parsed = FCSV.parse('C:/ruby/work/FRIEDEXT01.LFP', 'r')

Let's try changing the above line to:

parsed = FCSV.read('C:/ruby/work/FRIEDEXT01.LFP')

> changed = parsed.inject(Array.new) do |rows, row|
>   if row[2] == "C" and (d_row = rows.reverse.find { |r| r[2] == "D" })
>     d_row[2] = "P"
>   end
>   rows << row
> end
> puts changed.map { |r| r.to_csv }

Hope that helps.

James Edward Gray II
D3851c0aae6324a6d339cdefab56f452?d=identicon&s=25 Geoff (Guest)
on 2007-04-26 21:50
(Received via mailing list)
Excellent, thanks!

Now if only I understood all this better. :)

Geoff

On Apr 26, 9:37 am, James Edward Gray II <j...@grayproductions.net>
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-04-26 22:00
(Received via mailing list)
On Apr 26, 2007, at 2:50 PM, Geoff wrote:

> Now if only I understood all this better. :)

Feel free to ask questions.  I'll try to explain.

James Edward Gray II
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 William James (Guest)
on 2007-04-27 09:16
(Received via mailing list)
Geoff wrote:
> IM,0000003,D,0,@FRIEDEXT01;IMAGES\00\00;0000003.TIF;2
> IM,0000005,C,0,@FRIEDEXT01;IMAGES\00\00;0000005.TIF;2
> IM,0000006,D,0,@FRIEDEXT01;IMAGES\00\00;0000006.TIF;2
>
> Thank you,
>
> Geoff

data = IO.readlines("junk").map{|line| line.split(",")}.transpose
flag = nil
(data[2].size - 1).downto(0){|i|
  case data[2][i]
    when 'C'
      flag = true
    when 'D'
      data[2][i] = 'P' if flag
      flag = nil
  end
}
puts data.transpose.map{|a| a.join(",")}


--- input ---
IM,0000001,D,0,@FRIEDEXT01;IMAGES\00\00;0000001.TIF;2
IM,0000002, ,0,@FRIEDEXT01;IMAGES\00\00;0000002.TIF;2
IM,0000003,D,0,@FRIEDEXT01;IMAGES\00\00;0000003.TIF;2
IM,0000004, ,0,@FRIEDEXT01;IMAGES\00\00;0000004.TIF;2
IM,0000005,C,0,@FRIEDEXT01;IMAGES\00\00;0000005.TIF;2
IM,0000006,D,0,@FRIEDEXT01;IMAGES\00\00;0000006.TIF;2

--- output ---
IM,0000001,D,0,@FRIEDEXT01;IMAGES\00\00;0000001.TIF;2
IM,0000002, ,0,@FRIEDEXT01;IMAGES\00\00;0000002.TIF;2
IM,0000003,P,0,@FRIEDEXT01;IMAGES\00\00;0000003.TIF;2
IM,0000004, ,0,@FRIEDEXT01;IMAGES\00\00;0000004.TIF;2
IM,0000005,C,0,@FRIEDEXT01;IMAGES\00\00;0000005.TIF;2
IM,0000006,D,0,@FRIEDEXT01;IMAGES\00\00;0000006.TIF;2
This topic is locked and can not be replied to.