Can't puts out multiple lines
The following code opens a text file does a little regex to match names and开发者_如何学编程 numbers. I do an IF so that I only match numbers greater than 0. When I try to puts the name and number, I only get the numbers and the name have become nil. If I puts the names (variable a) before the if statement, it is there. What am I doing wrong?
nf = File.open("textfile.txt")
nf.each do |b|
a = b.match(/([\S]+)name([\S]+)/)
c = b.match(/[0-9]+ numbers/)
c = c.to_s.split(/ /)
c = c[0].to_i
if c > 0
puts a
puts c
end
end
textfile looks like this:
My name is Mark
12432 numbers My name is Joe 0 numbersI want to be able to puts:
My name is Mark 12432 numbersand not print out:
My name is Joe 0 numbersThanks in advance for your help
Well there are four lines. On lines 1 and 3 there is a name, so a will not be nil. However c > 0
will be false and thus nothing will be printed.
On lines 2 and 4 there is no name, so a
is nil.
Edit: Also /([\S]+)name([\S]+)/
will never actually match in your example file because "name" is surrounded by spaces (and \S
means "not a space").
Edit2: Here's a solution using scan
File.read("textfile.txt").scan(/My name is (\w+)\n(\d+) numbers/) do |name, num|
num = num.to_i
if num > 0
puts "Name: #{name}"
puts "Number: #{num}"
end
end
The problem is that you are trying to match both name and numbers in the same line.
A quick script that works, tested in irb:
File.open("foo", "r") do |file|
while name_line = file.gets
if name_line =~ /\s+name\s+/
number_line = file.gets # Here we read onto the next line
if match = number_line.match(/(\d+) numbers/)
# If matches, it will keep the number in the first capture group
puts name_line, match[1] if match[1].to_i > 0
end
end
end
end
Note that this will not work if two consecutive lines match "name" and the third one has "numbers", which may not be the case
p=""
File.readlines("file").each do |line|
num = line.split[0]
if num.to_i > 0
print "#{p}#{line}"
else
p=line
end
end
精彩评论