Forum: Ruby if statement not working with Hash object

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.
Mmcolli00 M. (Guest)
on 2009-01-21 00:19
Attachment: patient.txt (0 Bytes)
count1 = Hash.new(0)
File.open("patient.txt") do |src|
  src.each_line do |line|
    word1 = line
    count1[word1] += 1
    end
end

words = (count1.keys).uniq
words.each do |word|
  if word != /Date1|packageId/ then  #<----------does not evaluate, i am
not sure if i am using hash correctly

#check for an odd count, which means a discrepancy
     if (count1[word] == 1 or count1[word] == 3 or count1[word] == 5 or
count1[word] == 7  or count1[word] == 9 or count1[word] == 11 ) then
         puts "Discrepancy: #{word}"
         puts count1[word]
       end
   end
 end

#~ This goes through the patient.txt and counts how many occurrences
that there are of each line
#~ if there is an even number of the same line then there is no
discrepancy, if there is an odd number
#~ then that means the file broke somewhere and there is a discrepancy.
I am checking for odd values using hard code, this
#~ is because I could not get the .odd? method to work. My question is
this, how can I output everything that I have now except for
#~ any line containing values packageId and Date1?

#~ Here is my output but I want to leave out any line with the word
'packageId and Date1' because these will be irrelevant differences.

#~ Discrepancy: packageId 234 <- remove
#~ 1
#~ Discrepancy: Date1 03/10/08 <-remove
#~ 1
#~ Discrepancy: packageId 454 <-remove
#~ 1
#~ Discrepancy: Date1 03/11/08 <-remove
#~ 1
#~ Discrepancy: bloodtype o <---------------Leave only values like this
#~ 3
#~ Discrepancy: Date1 04/02/08 <-remove
#~ 1
#~ Discrepancy: Date1 03/01/08 <-remove
#~ 1
#~ Discrepancy: personId 23 <-remove
#~ 3
#~ Discrepancy: Date1 03/02/08 <-remove
#~ 1
#~ Discrepancy: packageId 2345 <-remove
#~ 1
#~ Discrepancy: packageId 2345 <-remove
#~ 1
Sebastian H. (Guest)
on 2009-01-21 00:28
(Received via mailing list)
Mmcolli00 Mom wrote:
> if word != /Date1|packageId/ then  #<----------does not evaluate, i am
> not sure if i am using hash correctly

This has nothing to do with hashes. word is a string /Date1|packageId/
is a
regexp. They will never be equal. I'm pretty sure you want !~ ("does not
match") instead of != ("is not equal to") there.

HTH,
Sebastian
Mmcolli00 M. (Guest)
on 2009-01-21 00:39
Sebastian H. wrote:
> Mmcolli00 Mom wrote:
>> if word != /Date1|packageId/ then  #<----------does not evaluate, i am
>> not sure if i am using hash correctly
>
> This has nothing to do with hashes. word is a string /Date1|packageId/
> is a
> regexp. They will never be equal. I'm pretty sure you want !~ ("does not
> match") instead of != ("is not equal to") there.
>
> HTH,
> Sebastian

I thought a reg expression could be used on any string. I am confused.
So if my Date1 has any value after it, I will have to have a string
wildcard, correct? What are you all using for wildcards? Thanks, MC
Sebastian H. (Guest)
on 2009-01-21 00:50
(Received via mailing list)
Mmcolli00 Mom wrote:
> I thought a reg expression could be used on any string.

It can. But "used on" and "compared to" are two different things.
"hello" != /hello/ #=> true
"hello" !~ /hello/ #=> false


> So if my Date1 has any value after it, I will have to have a string
> wildcard, correct? What are you all using for wildcards? Thanks, MC

I don't know what you mean.

HTH,
Sebastian
Mmcolli00 M. (Guest)
on 2009-01-21 04:12
>> So if my Date1 has any value after it, I will have to have a string
>> wildcard, correct? What are you all using for wildcards? Thanks, MC
>
> I don't know what you mean.

Well what I meant was that I don't understand how to check if a line in
the text file contains at least 'Date1'.

So anytime it the line has at least contains Date1, then it should be
ignored in my code. I thought that I could use the regular expression
such as:

if word != /Date1/

however, I can't use the reg ex and don't know how to do that with a
string. I was thinking there is a wild card so that I could use
something like: if word !~ "Date1*" with '*' being any value after
Date1.

Thanks, MC
Mmcolli00 M. (Guest)
on 2009-01-21 04:52
Mmcolli00 Mom wrote:

Oh my gosh! Please don't reply to this..I finally see how this actually
works on strings. doh!!! Thanks everyone for helping!! :-) MC
Robert K. (Guest)
on 2009-01-21 08:50
(Received via mailing list)
On 21.01.2009 03:11, Mmcolli00 Mom wrote:
>
> if word != /Date1/

if word !~ /Date1/

> however, I can't use the reg ex and don't know how to do that with a
> string. I was thinking there is a wild card so that I could use
> something like: if word !~ "Date1*" with '*' being any value after
> Date1.

Again, if you want to do regexp matching you need to use operators "=~",
"===" or "!~" but NOT "==" or "!="!  These are totally different things.

  robert
Bosko I. (Guest)
on 2009-01-21 09:30
(Received via mailing list)
On Jan 21, 3:11 am, Mmcolli00 Mom <removed_email_address@domain.invalid> wrote:
> such as:
> Posted viahttp://www.ruby-forum.com/.
If you want to check only whether line contains 'Date1' you can simply
use

line.include? 'Date1'
Simon K. (Guest)
on 2009-01-29 13:36
(Received via mailing list)
* Sebastian H. <removed_email_address@domain.invalid> (2009-01-20) schrieb:

> Mmcolli00 Mom wrote:
>> if word != /Date1|packageId/ then  #<----------does not evaluate, i am
>> not sure if i am using hash correctly
>
> This has nothing to do with hashes. word is a string /Date1|packageId/ is a
> regexp. They will never be equal. I'm pretty sure you want !~ ("does not
> match") instead of != ("is not equal to") there.

Shouldn't the statement above always be executed then?

mfg,                         simon .... l
Mmcolli00 M. (Guest)
on 2009-01-29 17:39
> Shouldn't the statement above always be executed then?
>
> mfg,                         simon .... l

Yes, you are right Simon. Thanks MC
Jarmo P. (Guest)
on 2009-01-31 15:35
Also, line:
words = (count1.keys).uniq

is not needed, since hashes won't have duplicate keys.

You could also refine your loop also somehow like this:

count1.each_pair do |word, count|
  if word !~ /Date1|packageId/ && [1,3,5,7,9,11].include?(count)
         puts "Discrepancy: #{word}"
         puts count
  end
end

Cheers,
Jarmo
This topic is locked and can not be replied to.