Silly Array Question

This will probably be a silly question, but I've been searching for
quite awhile now and have been unable to locate how to do this.

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/.

I believe you'll want to read the 123 and 4 into their arrays as you'd expect, and then use the zip method.

···

On Jan 22, 2007, at 12:17 PM, Mike Keller wrote:

Then turn it into one array that looks like.
1234

--
Andy Lester => andy@petdance.com => www.petdance.com => AIM:petdance

Mike Keller wrote:

This will probably be a silly question, but I've been searching for
quite awhile now and have been unable to locate how to do this.

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

a = [1,2,3] * 4

And the text file.
4

b = [4] * 4

Then turn it into one array that looks like.
1234

a.zip(b).map{|c| c.flatten}

I presume that's close enough :slight_smile:

···

--
Alex

Mike Keller wrote:

This will probably be a silly question, but I've been searching for
quite awhile now and have been unable to locate how to do this.

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.

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

···

--
Posted via http://www.ruby-forum.com/\.

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"]

···

On Tue, Jan 23, 2007 at 03:17:36AM +0900, Mike Keller wrote:

This will probably be a silly question, but I've been searching for
quite awhile now and have been unable to locate how to do this.

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.

--
Aaron Patterson
http://tenderlovemaking.com/

Andy Lester wrote:

I believe you'll want to read the 123 and 4 into their arrays as
you'd expect, and then use the zip method.

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.

···

--
Posted via http://www.ruby-forum.com/\.

Drew Olson wrote:

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

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 :slight_smile:

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.

···

--
Posted via http://www.ruby-forum.com/\.

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","")

···

--
Posted via http://www.ruby-forum.com/\.

Mike Keller wrote:

Alex Young wrote:
> a.zip(b).map{|c| c.flatten}

This causes a different problem, what this does is format the output
like:
123
4

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. :slight_smile:

Or with:

my_string.chomp

James Edward Gray II

···

On Jan 22, 2007, at 3:03 PM, Drew Olson wrote:

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","")

Phrogz wrote:

Mike Keller wrote:

Alex Young wrote:

a.zip(b).map{|c| c.flatten}

This causes a different problem, what this does is format the output
like:
123
4

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"]

That's where the difference comes in - I had
irb(main):002:0> a = [[1,2,3]]*4
=> [[1,2,3], [1,2,3], [1,2,3], [1,2,3]]

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

Or with:

my_string.chomp

James Edward Gray II

Doh, I feel like a moron! Lower codegolf scores, here I come... :slight_smile:

···

--
Posted via http://www.ruby-forum.com/\.