If statement not working with Hash object

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

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

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

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

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 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!! :slight_smile: MC

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

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

On Jan 21, 3:11 am, Mmcolli00 Mom [email protected] 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’

Shouldn’t the statement above always be executed then?

mfg, simon … l

Yes, you are right Simon. Thanks MC

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