[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

if statement not working with Hash object

Mmcolli00 Mom

1/20/2009 10:18:00 PM

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-...attachment/3187/p...

--
Posted via http://www.ruby-....

10 Answers

Sebastian Hungerecker

1/20/2009 10:26:00 PM

0

Mmcolli00 Mom wrote:
> 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: sepp2k@jabber.org
ICQ: 205544826

Mmcolli00 Mom

1/20/2009 10:38:00 PM

0

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

Sebastian Hungerecker

1/20/2009 10:48:00 PM

0

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

Mmcolli00 Mom

1/21/2009 2:11:00 AM

0


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

Robert Klemme

1/21/2009 6:47:00 AM

0

On 21.01.2009 03:11, Mmcolli00 Mom 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/

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

--
remember.guy do |as, often| as.you_can - without end

Bosko Ivanisevic

1/21/2009 7:27:00 AM

0

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

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

line.include? 'Date1'

Simon Krahnke

1/29/2009 11:12:00 AM

0

* 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

Mmcolli00 Mom

1/29/2009 3:37:00 PM

0


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

Yes, you are right Simon. Thanks MC
--
Posted via http://www.ruby-....

Jarmo Pertman

1/31/2009 1:39:00 PM

0

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
--
Posted via http://www.ruby-....

Joel

6/22/2013 4:41:00 PM

0

Bobbie Sellers <bliss-sf4ever@dslextreme.com> wrote:

>> Yeah, he's been posting in adp for a decade. He wasn't nearly so bad
>> off, in the beginning, but he clearly did a lot more acid since then.
>> To put it simply, he has brain damage.
>
> Don't blame acid for what may be natural brain dysfunction.
>
> Most of the crazy people I know like to blame their dysfunction on
>drugs but then refuse modern pyscho-theraputics that might make them
>more accessible to reason. I knew crazy people back in the 1950s when
>acid was unknown to the vast majority of drug users and most crazy
>people were lucky to get Thorazine.


To be clear, I'm not blaming *pure* *LSD* for his problems. Morning
Glory acid is known to damage the brain, though.

--
Joel Crump