From: Robert Dober Date: 2007-07-11T20:12:33+09:00 Subject: Re: if / else, how to include next line On 7/11/07, Marc Hoeppner wrote: > > > > > count = 0 > > ARGF.each do |line| ## treating a line at turn > > puts line unless count.zero? > > count = [0, count-1].max > > if line =~ /your keyword or something/ then > > puts line > > count = 8 > > end > > end > > > > Ok, I started with this one, but run into one problem - again probably > simple... > The way this is written it asks for a regexp as a keyword. Now, I do > have a text file, in which I have a number of key words, one in each > line. What I wasnt able to figure out thus far was how to change the > above code so that it would expect a variable instead of a regexp. > > When I defined a variable within the code var1 = 'KOG0019' and tried to I had no idea what you wanted exactly, here are some possibilites (a) always the same string if line == "KOG0019" (b) containing the same string if line =~ /KOG0019/ or my favorite idiom if /KOG0019/ === line (c) KOG followed by digits something if /KOG\d+/ === line (d) KOG and exactly 4 digits if /KOG\d{4}/ === line (e) KOG and some digits with no more text thereafter if /KOG\d+\s*$/ === line This should get you going;) if not try to learn more about Regexps; by scanning this list or reading the perl man page about regexps (the baisc stuff is the same and gets you a long way already). The ultimate guide seems to be Friedl's "Mastering Regular Expressions". Cheers Robert > use it as an argument ala if = var1 then it did something..., but > instead of finding the corresponding line and copying the following at > lines it copies like 12000 lines and puts the keyword in every second > line... > > /Marc > > -- > Posted via http://www.ruby-forum.com/. > > -- I always knew that one day Smalltalk would replace Java. I just didn't know it would be called Ruby -- Kent Beck