Forum: Ruby on Rails double array

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Johnathan W. (Guest)
on 2007-04-11 09:33
Hi,
I have an ASCII file that looks like:

1 0 3 0 4
4 3 2 9 8
3 0 9 9 7

I am able to store each whole line as an seperate array. But I cannot
find out how I can get the input of that file and store it into a double
array. For example, 1 would be array[0][0], and 7 would be array[4][2].

Thank you.
urbanus (Guest)
on 2007-04-11 15:40
(Received via mailing list)
Hi Johnathan,

> I have an ASCII file that looks like:
>
> 1 0 3 0 4
> 4 3 2 9 8
> 3 0 9 9 7
>
> I am able to store each whole line as an seperate array. But I cannot
> find out how I can get the input of that file and store it into a double
> array. For example, 1 would be array[0][0], and 7 would be array[4][2].

Are those spaces separating each item, or did you just put that in for
readability?  No worries, it's similar either way.

Assuming you've already used readlines() to read all the file's lines
into an array you can then do this:

array.map! do |line| line.split // end

Splitting on an empty regexp divides a string into individual
characters.  But for space-delimited characters you can just use the
default split using whitespace, which makes things even easier:

array.map! do |line| line.split end


Regards,
Dave
This topic is locked and can not be replied to.