I have two arrays which come from two different files. One is a CSV
file and the other just a list in a text document.
What I want to do is this.
Take the csv doc.
123
123
123
123
And the text file.
4
4
4
4
Then turn it into one array that looks like.
1234
1234
1234
1234
Maybe I've just been reading the stuff I've been finding on ruby-doc
wrong/not making the connection ( I have had a cold ) but any help that
could be provided would be appreciated.
--
Posted via http://www.ruby-forum.com/.
> Then turn it into one array that looks like.
> 1234
> 1234
> 1234
> 1234
I believe you'll want to read the 123 and 4 into their arrays as
you'd expect, and then use the zip method.
--
Andy Lester => an...@petdance.com => www.petdance.com => AIM:petdance
> And the text file.
> 4
> 4
> 4
> 4
b = [4] * 4
>
> Then turn it into one array that looks like.
> 1234
> 1234
> 1234
> 1234
a.zip(b).map{|c| c.flatten}
I presume that's close enough :-)
--
Alex
You are quite right I spent all this time looking at the ruby-doc for
arrays and completely missed it. Thank you so much for your help.
Assuming that the .csv file is coming in as a 2D array and that the text
document is a simple array, this should work:
csv_array.each_with_index do |row,index|
row << text_array[index]
end
You could try doing a zip with a map:
a = %w{ 123 123 123 123 }
b = %w{ 4 4 4 4 }
a.zip(b).map { |x,y| "#{x}#{y}" } # => ["1234", "1234", "1234", "1234"]
--
Aaron Patterson
http://tenderlovemaking.com/
The problem with this is that it lists the contents of the two files in
this order.
1
2
3
4
repeat
when I need it in
1234
1234
1234
1234
Alex Young wrote:
> a.zip(b).map{|c| c.flatten}
>
> I presume that's close enough :-)
This causes a different problem, what this does is format the output
like:
123
4
repeat
I believe that has to do with the new lines in the file. I'm not
certain how to strip those out. Or how to get around the fact that they
are there.
You can strip new lines like so:
my_string.sub("\n","")
>> I believe that has to do with the new lines in the file. I'm not
>> certain how to strip those out. Or how to get around the fact
>> that they
>> are there.
>
> You can strip new lines like so:
>
> my_string.sub("\n","")
Or with:
my_string.chomp
James Edward Gray II
Actually, it doesn't 'format' the output at all. Let's see what's going
on:
irb(main):002:0> a = %w{123 456 789 }
=> ["123", "456", "789"]
irb(main):003:0> b = %w{x y z}
=> ["x", "y", "z"]
irb(main):004:0> a.zip(b)
=> [["123", "x"], ["456", "y"], ["789", "z"]]
OK, so zipping the two arrays together produces an array, where each
piece is itself an array of the two values.
irb(main):005:0> a.zip(b).map{ |c| c.flatten }
=> [["123", "x"], ["456", "y"], ["789", "z"]]
Hrm...so this doesn't do anything, because they individual pieces were
already flattened.
irb(main):006:0> puts ["1", "2"]
1
2
irb(main):007:0> puts a.zip(b)[0]
123
x
Ah, when you pass an array to "puts", it puts each part of the array on
each line. (That's what you're seeing.)
Let's fix that.
irb(main):008:0> a.zip(b).map{ |pair| pair.join }
=> ["123x", "456y", "789z"]
irb(main):009:0> puts a.zip(b).map{ |pair| pair.join }
123x
456y
789z
There you go. :)
Doh, I feel like a moron! Lower codegolf scores, here I come... :)
> irb(main):003:0> b = %w{x y z}
> => ["x", "y", "z"]
> irb(main):004:0> a.zip(b)
> => [["123", "x"], ["456", "y"], ["789", "z"]]
I had
=> [[[1, 2, 3], 4], [[1, 2, 3], 4], [[1, 2, 3], 4], [[1, 2, 3], 4]]
which was why flattening was needed.
This is why it's good to see the actual data that's being worked on
sometimes...
--
Alex