Search an array element if it exist in a file

Hi All,

I’m a new to ruby. Can someone please explain how to search an array
list element exist in a file. scenario: i read a file.txt to get country
names and
them to an array list, from this array list i use each element to check
if they are being used in a myfile.txt, if they are not used i want to
add them to a not found array

Thanks

What have you tried so far? Have you searched Google for examples on how
to read text files and manipulate arrays?

i have tried this, but i still want to remove commas(,) at the end of
the array element.

list_of_selectors = []
file = File.open(“my.css”)
file.each_line do |line|
list_of_selectors << line.split(’ {’)[0] if line.start_with? ‘.’ or
line.start_with? ‘#’
end
while line = file.gets
puts line
end
i = 0
while i < list_of_selectors.length
puts “#{list_of_selectors[i]}”
i += 1
end
list = []
list_of_selectors.each { |x|
list.push(x.to_s.split(’ '))
}

list_of_selectors = list.flatten

puts “***************splitted ******************************”

puts list_of_selectors

puts “*********** split before dot *************************”

list_of_selectors.map! { |e| e[/[.#].*/]}

puts list_of_selectors

puts “**************remove duplicates **********************”

list_of_classes_ids = list_of_selectors.uniq
list_of_selectors.uniq!

puts list_of_selectors

puts “*Not found in my.js

jsfile= IO.read(“my.js”)
selectors_not_found = list_of_classes_ids.compact.reject {|class_id|
jsfile[class_id]}

puts selectors_not_found

list_of_selectors = (";"+IO.readlines(ARGV[0]).join(";")).
gsub(//*.?*//,"").
gsub(/;\s
;/,";").
scan(/;.*?{/).
each_with_object([]) { |line,a|
line.scan(/[#.][\w\d-_]+/).each { |class_id|
a << class_id[1…-1]}}.
uniq.
tap { |l| puts “Selectors:#{l.join(”, “)}” }

jsfile= IO.read(ARGV[1])
p list_of_selectors.reject {|class_id| jsfile=~ /#{class_id}/}

Thanks for your help Regis but i don’t want to remove the dot(.) and the
hashtag(#) before the selector name.

Thanks

Nonto Mb wrote in post #1146680:

I’m a new to ruby. Can someone please explain how to search an array
list element exist in a file. scenario: i read a file.txt to get country
names and
them to an array list, from this array list i use each element to check
if they are being used in a myfile.txt, if they are not used i want to
add them to a not found array

I may be wrong but what you describe seems easily done with shell tools:

$ fgrep -vf file.txt myfile.txt >not-found.txt

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs