From: Gawnsoft Date: 2003-08-02T01:33:07+09:00 Subject: Re: A bundle of newbie queries On Sat, 2 Aug 2003 00:44:24 +0900, Brian Candler wrote (more or less): >On Sat, Aug 02, 2003 at 12:12:39AM +0900, Gawnsoft wrote: >> On Fri, 1 Aug 2003 21:30:19 +0900, dblack@superlink.net wrote (more or >> less): >> >> >On Fri, 1 Aug 2003, Gawnsoft wrote: >> >>... >> >> aDictionary = Hash.new(0) >> >> >> >> aFile.each_line { | eachLine | aDictionary[ /[0-9.]+/ ] = >> >> aDictionary[ /[0-9.]+/ ] + 1 if >> >> eachLine.include?("plastic_1.1_lite-UMLtool-fw.exe") } >> >> >> > >> >Yes, you can use a regex as a key, but in your example, you're not >> >doing anything else with it :-) You'd get the same results with: >> > >> > dict[/blah/] = dict[/blah/] + 1 >> >> It was in IRB, so once I had the dictionary populated, I was also then >> able to >> aDictionary.each_value { | entry | puts entry } >> to see how often some people had downloaded the file and the like, so >> it wasn't entrirely wasted. > >It seems you are doing something strange. Consider these examples: > > h = {} > h[/blah/] = 1 > h[/blah/] = 1 > h[/blah/] = 1 > p h #>> {/blah/=>1, /blah/=>1, /blah/=>1} > >In other words, it looks like you are populating the hash with three >different Regex objects as the key. That's not very useful. You're absolutely right - I pasted in the wrong bits of code! (alternative phrasing, I pasted in bits of wrong code) Aargh. The corrected code reads: aFile.each_line { | line | aDictionary[line.slice(/[0-9.]+/)] = aDictionary[line.slice(/[0-9.]+/)] + 1 if eachLine.include?("plastic") } Thanks for pointing this out, and sorry to mislead. >Just to confuse things a bit: > > h = Hash.new(0) > 3.times { h[/blah/] = h[/blah/] + 1 } > p h #>> {/blah/=>1} > >(i.e. there are only two /blah/ objects this time: one is used as the key on >the left-hand side, and one as the key on the right-hand side) > >I think what you want is: > > h = Hash.new(0) > str = "wibble 1.2.3.4 bibble" > > /([0-9.]+)/ =~ str > h[$1] += 1 if $1 > > p h #>> {"1.2.3.4"=>1} > >You also have to beware of free-standing regular expressions, because they >have some perlish side-effects in certain circumstances (inside >conditionals, I think): > > gets # assigns to $_ as a side-effect > if /([0-9.]+)/ # matches against $_ by default > h[$1] += 1 > end > >This usage is uncouth and deprecated. > >> >or even: >> > >> > dict[/blah/] = dict["hello!"] + 1 >> >> Hmmm - wouldn't this result in aDictionary having an entry with a key >> of "hello!" and so through my count of IP numbers off by one? > >No - unlike Perl, reading from a hash element which does not exist does not >cause that element to be created. Example: > > h = Hash.new(99) > p h["foo"] #>> 99 -- default value returned > p h #>> {} -- but the hash was not modified > >Regards, > >Brian. Cheers, Euan Gawnsoft: http://www.gawnsoft.co.sr Symbian/Epoc wiki: http://html.dnsalias.net:1122 Smalltalk links (harvested from comp.lang.smalltalk) http://html.dnsalias.net/gawnsoft/smalltalk