[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Silly Array Question

Mike Keller

1/22/2007 6:18:00 PM

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

11 Answers

andy

1/22/2007 6:21:00 PM

0


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

> 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 => andy@petdance.com => www.petdance.com => AIM:petdance





Alex Young

1/22/2007 6:21:00 PM

0

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

--
Alex

Mike Keller

1/22/2007 6:23:00 PM

0

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

Drew Olson

1/22/2007 6:24:00 PM

0

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

Aaron Patterson

1/22/2007 6:26:00 PM

0

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.

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

Mike Keller

1/22/2007 8:57:00 PM

0

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

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

Drew Olson

1/22/2007 9:04:00 PM

0

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

James Gray

1/22/2007 9:14:00 PM

0

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

Or with:

my_string.chomp

James Edward Gray II


Gavin Kistner

1/22/2007 10:52:00 PM

0

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

Drew Olson

1/22/2007 11:06:00 PM

0

> Or with:
>
> my_string.chomp
>
> James Edward Gray II

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

--
Posted via http://www.ruby-....