[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

what's the problem with array.include?

music

4/8/2007 6:45:00 PM

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?
6 Answers

Xavier Noria

4/8/2007 6:57:00 PM

0

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 Johnson

4/8/2007 7:01:00 PM

0

On 4/8/07, music <music@musi.ca> 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

4/8/2007 7:56:00 PM

0

Lyle Johnson wrote:
> On 4/8/07, music <music@musi.ca> 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).
>

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:
prova@azienda.itprova1@nonloso.it@utente1@azienda.it

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

this is the content of arr.txt file:
ancora@nonloso.itchi@azienda.itprova@azienda.it

as you see prova@azienda.it is in hash and in arr then it is in @hash
and in @a but @a.include?(value) it doesn't find it.

Xavier Noria

4/8/2007 8:15:00 PM

0

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

> as you see prova@azienda.it 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

4/8/2007 8:40:00 PM

0

Xavier Noria wrote:
> On Apr 8, 2007, at 9:55 PM, music wrote:
>
>> as you see prova@azienda.it 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 Noria

4/8/2007 9:16:00 PM

0

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

> Xavier Noria wrote:
>> On Apr 8, 2007, at 9:55 PM, music wrote:
>>> as you see prova@azienda.it 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?

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