class MyNewClass @hash = {} @arr = [] @a = []
IO.foreach(“file.txt”) do |riga|
codice,cognome,nome,servizio,mail = riga.chomp.split(/\t/) @hash.store("#{mail}","#{cognome} #{nome}")
end
IO.foreach(“mail.log”) do |riga1|
if riga1.match(/Passed/)
@a=riga1.scan(/<(.?@.?)>|(?)/) @hash.each_key do |mail|
if @a.include?(mail)
puts “#{mail} found”
elsif
puts “not found!!!”
end
end
end
end
end
@a.include?(mail) it seems don’t work.
I’m sure that there is at least one value true, but the result is always
“not found”.
What is the problem?
@a = []
IO.foreach(“file.txt”) do |riga|
codice,cognome,nome,servizio,mail = riga.chomp.split(/\t/) @hash.store("#{mail}","#{cognome} #{nome}")
end
IO.foreach(“mail.log”) do |riga1|
if riga1.match(/Passed/)
@a=riga1.scan(/<(.?@.?)>|(?)/) @hash.each_key do |mail|
if @a.include?(mail)
@a.include?(mail) it seems don’t work.
I’m sure that there is at least one value true, but the result is
always “not found”.
What is the problem?
@a stores something different from what you seem to think, print it.
Is there any chance that there’s some additional whitespace around the
e-mail address, either in the first file (“file.txt”) or the log file
(“mail.log”)? That would probably be enough to cause include? not to
find the match (since it’s looking for an exact match).
here is some hash.key values from:
@file=File.new(“hash.txt”, “w+”) @hash.each_key {|x| @file.write x}
@a.include?(mail) it seems don’t work.
I’m sure that there is at least one value true, but the result is always
“not found”. What is the problem?
Is there any chance that there’s some additional whitespace around the
e-mail address, either in the first file (“file.txt”) or the log file
(“mail.log”)? That would probably be enough to cause include? not to
find the match (since it’s looking for an exact match).
as you see [email protected] is in hash and in arr then it is in @hash
and in @a but @a.include?(value) it doesn’t find it.
As I said before please inspect @a, its elements are not what you think.
As a hint
puts @a.first.class
This returns Array, it seems the correct answer I think.
Then read the docs of String#scan to understand why you are after
something like
@a.flatten.compact
Great…with @a=riga1.scan(/<(.?@.?)>|(?)/).flatten.compact in
place of
@a=riga1.scan(/<(.?@.?)>|(?)/) puts @a.first.class returns String
and the code now works well.
Sorry but I’m newbie on ruby, can you explain why it works now?
Thank you.
Then read the docs of String#scan to understand why you are after
something like @a.flatten.compact
Great…with @a=riga1.scan(/<(.?@.?)>|(?)/).flatten.compact
in place of
@a=riga1.scan(/<(.?@.?)>|(?)/) puts @a.first.class returns
String and the code now works well.
Sorry but I’m newbie on ruby, can you explain why it works now?
When the regexp has groups String#scan returns an array of arrays,
each one consisting of the corresponding captures: