Robert Klemme
7/27/2008 4:59:00 PM
On 27.07.2008 18:52, Thomas Luedeke wrote:
> I'm trying to do something that I think would be pretty trivial. I've
> read in an external file as an array. I want to parse the array of
> lines, and set a line number as a variable if the line matches a certain
> pattern. The file is rather long, so I want to break out of the loop
> once I've made the match.
>
> I've tried this a number of different ways, and they either don't work,
> or I get some JumpError (which is incomprehensible to my newbie mind).
> I'm still deeply in the "suck period" of learning Ruby (and
> transitioning from O-O Fortran 95 and UNIX scripting), so I'm struggling
> quite a bit.
>
> I'm running Ruby 1.8.6 through Eclipse.
>
> Where I'm at is the following (and this is just the latest in a series
> of varieties trying to make the stupid thing work):
>
> ============================
>
> inputFile = File.open( "filename","r" )
> inputArray = inputFile.readlines
> counts = 1
> xcob3cArray.each catch(:breakout) do |line|
> line_number = counts
> throw(:breakout) "$counts" if ( line.lstrip =~ /^(11))/ )
> counts = counts + 1
> end
> line_number = $counts
>
> ============================
>
> Any help would be greatly appreciated!!
If your file is really that large you'd probably rather want to read it
line by line and not in a single Array.
# untested
def search file, rx
File.open file do |io|
io.each do |line|
return io.lineno if rx =~ line
end
end
end
Kind regards
robert