From: Robert Klemme Date: 2003-10-02T19:05:36+09:00 Subject: Re: Array and hash iteration questions "Robert Klemme" schrieb im Newsbeitrag news:blgp2a$bvnb8$1@ID-52924.news.uni-berlin.de... > > "Ben Giddings" schrieb im Newsbeitrag > news:3F7B0BAC.7030305@infofiend.com... > > Robert Klemme wrote: > > > cases = Hash.new {|h,k| h[k] = [0, 0]} > > > > Ah. I couldn't remember how to use the block form properly. I'm > actually > > going to use: > > > > cases = Hash.new {|hash, key| hash[key] = Hash.new(0)} > > > > Because it will make some of the later stuff more clear like > > > > cases[case]['Number'] += 1 > > cases[case]['Errors'] += 1 if arr[OFFSET] =~ /Error/ > > No need to use a Hash for this... > > Number = 0 > Errors = 1 > > cases[case][Number] += 1 > cases[case][Errors] += 1 if arr[OFFSET] =~ /Error/ > > I might be a bit pricky, but storing the array ref saves one hash lookup. > It *can* affect performance if you have a large amount of cases... (see > below; although the timing is dominated by the iteration here, you can see > that the array is faster) This sentence should really have appeared several lines above: it's the argument in favour of using arrays instead of hashes for the counters. Regards robert