csv next and previous help

G

Geoff

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
 
J

James Edward Gray II

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. ;)
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"=> [["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"]]?> 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"]]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
 
G

Ganesh Gunasegaran

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
 
G

Geoff

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

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. ;)
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"=> [["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"]]?> 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"]]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
 
J

James Edward Gray II

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
 
G

Geoff

Excellent, thanks!

Now if only I understood all this better. :)

Geoff

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
 
W

William James

Geoff said:
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

data = IO.readlines("junk").map{|line| line.split(",")}.transpose
flag = nil
(data[2].size - 1).downto(0){|i|
case data[2]
when 'C'
flag = true
when 'D'
data[2] = '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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top