B
Benjamin Thomas
Hello all,
I write scripts for fun and am still a noob.
I'd like to use ruby to solve a problem for me but find it difficult
this time so I thought I'd ask for help.
It's very basic in fact.
I have 2 csv files I'd like to merge such as :
File1 File2
----- -----
ref;qty ref;price
A;10 A;100
B;20 D;150
C;30 C;200
E;5 B;75
outputs to File3 =>
File3
-----
ref;qty;price;total
A;10;100;1000
B;20;75;1500
C;30;200;6000
D;"missing_data";150;"missing_data"
E;5;"missing_data";"missing_data"
Here is the code I have so far:
###########################################################################
def process_file(file)
processed = []
opened = File.open(file, "r").readlines
opened.each {|line| processed << line.strip.split(";")}
return processed
end
arr1 = process_file("file1")
arr2 = process_file("file2")
p arr1, arr2
###########################################################################
==> [["ref", "qty"], ["A", "10"], ["B", "20"], ["C", "30"], ["E", "5"]]
==> [["ref", "price"], ["A", "100"], ["D", "150"], ["C", "200"], ["B",
"75"]]
----
So I have 2 arrays which is great but I'm not sure on how to go about
merging all this since data is sometimes missing from file1, some other
times from file2. Of course those 2 files are more complex (and
bigger!), and I'd like to setup a mechanism that would allow me to
select which columns to filter and merge, but that's the basic idea
anyway.
Thanks for your input and direction.
I write scripts for fun and am still a noob.
I'd like to use ruby to solve a problem for me but find it difficult
this time so I thought I'd ask for help.
It's very basic in fact.
I have 2 csv files I'd like to merge such as :
File1 File2
----- -----
ref;qty ref;price
A;10 A;100
B;20 D;150
C;30 C;200
E;5 B;75
outputs to File3 =>
File3
-----
ref;qty;price;total
A;10;100;1000
B;20;75;1500
C;30;200;6000
D;"missing_data";150;"missing_data"
E;5;"missing_data";"missing_data"
Here is the code I have so far:
###########################################################################
def process_file(file)
processed = []
opened = File.open(file, "r").readlines
opened.each {|line| processed << line.strip.split(";")}
return processed
end
arr1 = process_file("file1")
arr2 = process_file("file2")
p arr1, arr2
###########################################################################
==> [["ref", "qty"], ["A", "10"], ["B", "20"], ["C", "30"], ["E", "5"]]
==> [["ref", "price"], ["A", "100"], ["D", "150"], ["C", "200"], ["B",
"75"]]
----
So I have 2 arrays which is great but I'm not sure on how to go about
merging all this since data is sometimes missing from file1, some other
times from file2. Of course those 2 files are more complex (and
bigger!), and I'd like to setup a mechanism that would allow me to
select which columns to filter and merge, but that's the basic idea
anyway.
Thanks for your input and direction.