[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

split file into 2D array

pustoi

10/9/2004 3:41:00 PM

Hello,

I have file like this (words.txt):

#
word1 word2
word3 word4
word5 word6
word7 word8

How can I split words into 2D array in ruby ?

I just tried:

a = [[],[]]
IO.foreach("words.txt") {|line|
unless line.strip.empty? | (line =~ (/^\#/))
string = line.chomp.split
a << string
end
}

#puts a
puts [0][1]

[root@tequila2 logs]# ./logs5.rb
nil

If I tried to print second element from array (it should be word2),I
have got nil.

Thank you in advance
4 Answers

Brian Candler

10/9/2004 3:50:00 PM

0

On Sun, Oct 10, 2004 at 12:44:50AM +0900, Arthur Korneyew wrote:
> I just tried:
>
> a = [[],[]]
> IO.foreach("words.txt") {|line|
> unless line.strip.empty? | (line =~ (/^\#/))
> string = line.chomp.split
> a << string
> end
> }
>
> #puts a
> puts [0][1]
>
> [root@tequila2 logs]# ./logs5.rb
> nil
>
> If I tried to print second element from array (it should be word2),I
> have got nil.

Useful debug tool: put at the end

puts a.inspect
or
p a

and you'll see what's wrong. It's find except you put an initial empty row
in there. Either change the first line to

a = [] # array with no elements

or else do puts a[1][0], a[1][1] to get the first useful element.

Regards,

Brian.


Brian Candler

10/9/2004 4:06:00 PM

0

On Sun, Oct 10, 2004 at 12:50:10AM +0900, Brian Candler wrote:
> or else do puts a[1][0], a[1][1] to get the first useful element.

I mean puts a[2][0], a[2][1]

Since you initialized a=[[],[]]

then you have:

a[0] == []
a[1] == []
a[2] == ["word1", "word2"]

etc.


Tim Hunter

10/9/2004 4:08:00 PM

0

Arthur Korneyew wrote:

> Hello,
>
> I have file like this (words.txt):
>
> #
> word1 word2
> word3 word4
> word5 word6
> word7 word8
>
> How can I split words into 2D array in ruby ?
>
> I just tried:
>
> a = [[],[]]
> IO.foreach("words.txt") {|line|
> unless line.strip.empty? | (line =~ (/^\#/))
> string = line.chomp.split
> a << string
> end
> }
>
> #puts a
> puts [0][1]
>
> [root@tequila2 logs]# ./logs5.rb
> nil
>
> If I tried to print second element from array (it should be word2),I
> have got nil.
>
> Thank you in advance

words = Array.new
DATA.each do |line|
line.chomp!
next if line == '' || line == '#'
words << line.split(' ')
end

puts words[0][1] # prints "word2"
__END__
#
word1 word2
word3 word4
word5 word6
word7 word8


Mark Hubbart

10/9/2004 6:48:00 PM

0

On Sun, 10 Oct 2004 00:44:50 +0900, Arthur Korneyew <pustoi@spils.lv> wrote:
> Hello,
>
> I have file like this (words.txt):
>
> #
> word1 word2
> word3 word4
> word5 word6
> word7 word8
>
> How can I split words into 2D array in ruby ?
>
> I just tried:
>
> a = [[],[]]
> IO.foreach("words.txt") {|line|
> unless line.strip.empty? | (line =~ (/^\#/))
> string = line.chomp.split
> a << string
> end
> }
>
> #puts a
> puts [0][1]
>
> [root@tequila2 logs]# ./logs5.rb
> nil

The problem, as others have shown, is that you did more array
initialization than you needed. But there are also simpler ways of
doing this, which you may not have known about:

IO (and File, and String), are enumerable, like arrays and ranges.
Instead of iterating through each element, IO and String objects go
through each line. This gives you the ability to use #map on a File,
which converts it to an array of lines:

# block form automatically closes the file after the block finishes
words = File.open("words") do |file|
# file.map passes in each line, then line.split splits it into an array.
file.map{|line| line.split}
end

It appears that you want to get rid of lines that don't have exactly
two words in them, so add this line after you get the array of words:

# reject! deletes the element if the block evaluates to true
# in this case, it deletes the element if it doesn't contain two items
words.reject!{|element| element.size != 2}

hth,
Mark

>
> If I tried to print second element from array (it should be word2),I
> have got nil.
>
> Thank you in advance
>
>