# If statement not working with Hash object

**URL:** <https://rubytalk.org/t/if-statement-not-working-with-hash-object/51285>\
**Category:** ruby-talk\
**Created:** [20 January 2009 22:17 UTC](https://rubytalk.org/t/if-statement-not-working-with-hash-object/51285 "2009-01-20T22:17:40Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mmcolli00\_Mom](https://avatars.discourse-cdn.com/v4/letter/m/76d3ee/32.png) [@Mmcolli00\_Mom](https://rubytalk.org/u/Mmcolli00_Mom)\
**Post date:** [20 January 2009 22:17 UTC](https://rubytalk.org/t/if-statement-not-working-with-hash-object/51285/1 "2009-01-20T22:17:40Z")

</div>

count1 = Hash.new(0)  
File.open("patient.txt") do |src|  
&nbsp;&nbsp;src.each\_line do |line|  
&nbsp;&nbsp;&nbsp;&nbsp;word1 = line  
&nbsp;&nbsp;&nbsp;&nbsp;count1[word1] += 1  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end

words = (count1.keys).uniq  
words.each do |word|  
&nbsp;&nbsp;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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Discrepancy: #{word}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts count1[word]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;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](http://www.ruby-forum.com/attachment/3187/patient.txt)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)\
**Post date:** [20 January 2009 22:25 UTC](https://rubytalk.org/t/if-statement-not-working-with-hash-object/51285/2 "2009-01-20T22:25:58Z")

</div>

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

> **···**
>
> --  
> NP: In Flames - Coerced Coexistence  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826

---

<div class="post-metadata">

**Author:** ![Mmcolli00\_Mom](https://avatars.discourse-cdn.com/v4/letter/m/76d3ee/32.png) [@Mmcolli00\_Mom](https://rubytalk.org/u/Mmcolli00_Mom)\
**Post date:** [20 January 2009 22:38 UTC](https://rubytalk.org/t/if-statement-not-working-with-hash-object/51285/3 "2009-01-20T22:38:00Z")

</div>

Sebastian Hungerecker 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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Simon\_Krahnke](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Simon\_Krahnke](https://rubytalk.org/u/Simon_Krahnke)\
**Post date:** [29 January 2009 11:32 UTC](https://rubytalk.org/t/if-statement-not-working-with-hash-object/51285/4 "2009-01-29T11:32:44Z")

</div>

\* Sebastian Hungerecker \<sepp2k@googlemail.com\> (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

---

<div class="post-metadata">

**Author:** ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)\
**Post date:** [20 January 2009 22:48 UTC](https://rubytalk.org/t/if-statement-not-working-with-hash-object/51285/5 "2009-01-20T22:48:26Z")

</div>

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

> **···**
>
> --  
> NP: Depeche Mode - Strangelove  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826

---

<div class="post-metadata">

**Author:** ![Mmcolli00\_Mom](https://avatars.discourse-cdn.com/v4/letter/m/76d3ee/32.png) [@Mmcolli00\_Mom](https://rubytalk.org/u/Mmcolli00_Mom)\
**Post date:** [29 January 2009 15:36 UTC](https://rubytalk.org/t/if-statement-not-working-with-hash-object/51285/6 "2009-01-29T15:36:53Z")

</div>

> Shouldn't the statement above always be executed then?
> 
> mfg, simon .... l

Yes, you are right Simon. Thanks MC

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Mmcolli00\_Mom](https://avatars.discourse-cdn.com/v4/letter/m/76d3ee/32.png) [@Mmcolli00\_Mom](https://rubytalk.org/u/Mmcolli00_Mom)\
**Post date:** [21 January 2009 02:11 UTC](https://rubytalk.org/t/if-statement-not-working-with-hash-object/51285/7 "2009-01-21T02:11:06Z")

</div>

> > 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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Jarmo\_Pertman](https://avatars.discourse-cdn.com/v4/letter/j/2acd7d/32.png) [@Jarmo\_Pertman](https://rubytalk.org/u/Jarmo_Pertman)\
**Post date:** [31 January 2009 13:39 UTC](https://rubytalk.org/t/if-statement-not-working-with-hash-object/51285/8 "2009-01-31T13:39:15Z")

</div>

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|  
&nbsp;&nbsp;if word !~ /Date1|packageId/ && [1,3,5,7,9,11].include?(count)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Discrepancy: #{word}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts count  
&nbsp;&nbsp;end  
end

Cheers,  
Jarmo

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [21 January 2009 06:48 UTC](https://rubytalk.org/t/if-statement-not-working-with-hash-object/51285/9 "2009-01-21T06:48:40Z")

</div>

> > > 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/

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.

&nbsp;&nbsp;robert

> **···**
>
> On 21.01.2009 03:11, Mmcolli00 Mom wrote:
> 
> --  
> remember.guy do |as, often| as.you\_can - without end

---

<div class="post-metadata">

**Author:** ![Bosko\_Ivanisevic](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@Bosko\_Ivanisevic](https://rubytalk.org/u/Bosko_Ivanisevic)\
**Post date:** [21 January 2009 07:28 UTC](https://rubytalk.org/t/if-statement-not-working-with-hash-object/51285/10 "2009-01-21T07:28:24Z")

</div>

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

line.include? 'Date1'

> **···**
>
> On Jan 21, 3:11 am, Mmcolli00 Mom \<mmc\_coll...@yahoo.com\> wrote:
> 
> > \>\> 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  
> > --  
> > Posted viahttp://www.ruby-forum.com/.
