Doesn't quite work...looping question

It parses, it opens, but it doesn’t write to the outFile. So I am
guessing I am not looping through the array properly.

#!/usr/bin/ruby -w

populate the array

kw = [ “adult”, “teen” ]

create a file to append data found from the input file

inFile = File.open(ARGV[0], “r”)
outFile = File.new(ARGV[1], “w+”)

search for a word and print the entire line

inFile.each_line { |line|
for kw in line # keywords to search for
print line # let the lines print to console
outFile.write line # also print to file
end
}

close the files

inFile.close
outFile.close

visual clue script is done

puts “Finished processing file…”

Bob X bobx@linuxmail.org writes:

It parses, it opens, but it doesn’t write to the outFile. So I am
guessing I am not looping through the array properly.

#!/usr/bin/ruby -w

populate the array

kw = [ “adult”, “teen” ]

create a file to append data found from the input file

inFile = File.open(ARGV[0], “r”)
outFile = File.new(ARGV[1], “w+”)

search for a word and print the entire line

inFile.each_line { |line|
for kw in line # keywords to search for

THis overwrites your kw array with ‘line’

Try something like (untested)

 for word in kw
   if line.include?(word)
print line	  # let the lines print to console
outFile.write line	# also print to file
  end
 end

Cheers

Dave

Dave Thomas wrote:

Try something like (untested)

 for word in kw
   if line.include?(word)
  print line	  # let the lines print to console

outFile.write line # also print to file
end
end

Got me where I needed to go…! :slight_smile: