Forum: Ruby what's the problem with array.include?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
music (Guest)
on 2007-04-08 22:46
(Received via mailing list)
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?
Xavier N. (Guest)
on 2007-04-08 22:58
(Received via mailing list)
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)

<snip>

> @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. (Guest)
on 2007-04-08 23:02
(Received via mailing list)
On 4/8/07, music <removed_email_address@domain.invalid> wrote:

> Here is my code:

<snip>

> @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).
music (Guest)
on 2007-04-08 23:55
(Received via mailing list)
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:
removed_email_address@domain.invalidremoved_email_address@domain.invalid@removed_email_address@domain.invalid

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

this is the content of arr.txt file:
removed_email_address@domain.invalid@removed_email_address@domain.invalid

as you see removed_email_address@domain.invalid is in hash and in arr then it is 
in @hash
and in @a but @a.include?(value) it doesn't find it.
Xavier N. (Guest)
on 2007-04-09 00:16
(Received via mailing list)
On Apr 8, 2007, at 9:55 PM, music wrote:

> as you see removed_email_address@domain.invalid 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
music (Guest)
on 2007-04-09 00:42
(Received via mailing list)
Xavier N. wrote:
> On Apr 8, 2007, at 9:55 PM, music wrote:
>
>> as you see removed_email_address@domain.invalid 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.
Xavier N. (Guest)
on 2007-04-09 01:17
(Received via mailing list)
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
This topic is locked and can not be replied to.