The following code opens a text file, makes a small regex to match names and numbers. I do so that I only match more than 0 numbers. When I put names and numbers, then I get only numbers and the name is zero. If I put the name (variable A) before the event statement, then what is it that I am doing?
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 & gt; 0 adds a C & End
text file looks like this:
My name is Mark
12432 Number
My name is number which is < / P>
I want to say:
My name is Mark - 12432 Number
And do not print:
My name is that
0 points
Thanks in advance for your help
OK, there are four lines at 1 and 3 rows , So there will be no zero though the c & gt; 0
will be wrong and thus nothing will be printed.
There is no name on line 2 and 4, so a
is zero.
Edit: Furthermore, the / ([\ S] +) name ([\ S] +) /
will never match in your example file because "name" is empty The location (and s
means "one place" not )
Edit 2: Here scan
file. Read ("textfile.txt"). Scan (/ My name is (\ w +) \ n (\ d +) number /) What to do. Name, number Num = num.to_i if num & gt; 0 adds "name: # {name}" to "number: # {num}" ending
Comments
Post a Comment