Silly Array Question

M

Mike Keller

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

Andy Lester

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

Alex Young

Mike said:
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
a = [1,2,3] * 4
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 :)
 
M

Mike Keller

Andy said:
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.
 
D

Drew Olson

Mike said:
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
 
A

Aaron Patterson

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.

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

Mike Keller

Drew said:
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 said:
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.
 
D

Drew Olson

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

Phrogz

Mike said:
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. :)
 
A

Alex Young

Phrogz said:
Mike said:
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...
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top