split file into 2D array

  • Thread starter Arthur Korneyew
  • Start date
A

Arthur Korneyew

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
 
B

Brian Candler

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

Brian Candler

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

Tim Hunter

Arthur said:
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
 
M

Mark Hubbart

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
 

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

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top