From: Paul Lutus Date: 2006-09-16T00:45:53+09:00 Subject: Re: Need help about Ruby with text file Oscar Lok wrote: / ... >> And now, on re-reading his first post, I can't tell whether he got that >> result and didn't want it, or didn't get it and wanted it. Or ... got it >> and wanted it, and thought he would mention it in passing. > > Hi,Paul Lutus > Thanks for your reply! > > aabbcc > ddeeff > gghhii > jjkkll > > 2)I get the result like :- > hh > > * What I would like to do is :- > 1) go through each line from the begin > 2) compare "hh" each letter on each line > 3) found the line number of "gghhii" > 4) extract "hh" of it. Please provide an example. I think you don't literally mean you want 'hh' specifically, but these are meant to stand in for the actual characters you want. Yes? Otherwise this would work: ------------------------------------------- #!/usr/bin/ruby data = File.read("example.txt") n = 0 data.each do |line| test = line.sub(/..(..)../,"\\1") print "#{n}: #{test}\n" if test =~ /hh/ n += 1 end ------------------------------------------- Example file contains: aabbcc ddeeff gghhii jjkkll Output: 2: hh Conclusion? You simply must explain what you want, clearly enough that it can be turned into code. -- Paul Lutus http://www.arachnoid.com