Rubic wrote:
I new to Ruby and I have lost touch with whatever little programming I
did know a few years back. Sorry if this is a lame question.
How does one nest arrays?
One possibility:
array = [ [“foo”, “bar”], [“baz”, 42] ]
I want to check every element of the array and compare it against rest
of the elements of that array for repeat occurences. but for now I did
be happy if I can get to even print the 2 values, hence ‘puts #i, #j’ in
the code.
f = File.open(“iplist.txt”)
irb(main):011:0> f.class
=> File
f1 = File.open(“iplist.txt”)
irb(main):011:0> f1.class
=> File
f.each { |i|
f1.each { |j|
puts “#{i}, #{j}”
}
}
If you want an Array, use File#readlines. This produces an array of all
lines in a file (including the \n at the end of each line), and you can
iterate over them. For example:
C:>ruby test.rb
line1,line1
line1,line2
line1,line3
line2,line1
line2,line2
line2,line3
line3,line1
line3,line2
line3,line3
C:>cat test.rb
#Create Arrays for the files:
f = File.readlines(“test.txt”)
f1 = File.readlines(“test.txt”)
#Iterate over the Arrays:
f.each do |i|
f1.each do |j|
puts “#{i.chomp},#{j.chomp}”
end
end
With the above code I only get output of one successful iteration of
f.each and it stops and doesnt continue for the rest of the values of
f.each.
Probably because you have one big string in the file, and thus only one
object the block can iterate.
But without sample data, I can’t say more, really.
Surprisingly this works…
(0…5).each { |i|
(0…4).each { |j|
puts “#{i}, #{j}”
}
}
[snip]
whats the difference between the codes thats causing this error? They
are both arrays aint it?
0…5 and 0…4 are Ranges, not Arrays, and thus #each iterates over each
element of the Range.
–
Phillip “CynicalRyan” Gawlowski
http://cynicalryan.110mb.com/
Rule of Open-Source Programming #7:
Release early, release often. Clean compilation is optional.