What's the problem with array.include?

Here is my code:

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?

On Apr 8, 2007, at 8:45 PM, music wrote:

@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.

– fxn

Lyle J. wrote:

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}

this is the content of hash.txt:
[email protected][email protected]@[email protected]

and
@file=File.new(“arr.txt”, “w+”)
@a.each {|x| @file.write x}

this is the content of arr.txt file:
[email protected]@[email protected]

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.

On 4/8/07, music [email protected] wrote:

Here is my code:

@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).

On Apr 8, 2007, at 9:55 PM, music wrote:

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

Then read the docs of String#scan to understand why you are after
something like

@a.flatten.compact

– fxn

Xavier N. wrote:

On Apr 8, 2007, at 9:55 PM, music wrote:

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.

On Apr 8, 2007, at 10:40 PM, music wrote:

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:

irb(main):002:0> “foo bar”.scan(/\b(\S)|(\S)\b/)
=> [[“f”, nil], [nil, “o”], [“b”, nil], [nil, “r”]]

Those nils come from the fact that even if the pipe guarantees only
one side will match, the regexp as such still has two groups.

When there are no groups you get an array of strings with the actual
matches

irb(main):003:0> “foo bar baz”.scan(/\w+/)
=> [“foo”, “bar”, “baz”]

which I guess is what you thought @a contained.

– fxn