if statement not working with Hash object

M

Mmcolli00 Mom

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

Attachments:
http://www.ruby-forum.com/attachment/3187/patient.txt
 
S

Sebastian Hungerecker

Mmcolli00 said:
if word !=3D /Date1|packageId/ then =C2=A0#<----------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=
=20
regexp. They will never be equal. I'm pretty sure you want !~ ("does not=20
match") instead of !=3D ("is not equal to") there.

HTH,
Sebastian
=2D-=20
NP: In Flames - Coerced Coexistence
Jabber: (e-mail address removed)
ICQ: 205544826
 
M

Mmcolli00 Mom

Sebastian said:
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
 
S

Sebastian Hungerecker

Mmcolli00 said:
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
 
M

Mmcolli00 Mom

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
 
R

Robert Klemme

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/

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
 
B

Bosko Ivanisevic

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

If you want to check only whether line contains 'Date1' you can simply
use

line.include? 'Date1'
 
S

Simon Krahnke

* Sebastian Hungerecker said:
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
 
J

Jarmo Pertman

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top