# Most compact command for associate array 'totalling'?

**URL:** <https://www.ruby-forum.com/t/most-compact-command-for-associate-array-totalling/145322>\
**Category:** Ruby\
**Created:** [August 8, 2008, 12:01am UTC](https://www.ruby-forum.com/t/most-compact-command-for-associate-array-totalling/145322 "2008-08-08T00:01:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [August 8, 2008, 12:01am UTC](https://www.ruby-forum.com/t/most-compact-command-for-associate-array-totalling/145322/1 "2008-08-08T00:01:03Z")

</div>

Ok - in ‘awk’ you can do this: (Where ‘array’ is empty initially)

array[]++

For instance:

array[“abc”]++; =\> a[“abc”]=1  
array[“abc”]++; =\> a["abc’]=2  
…

Which is really useful for tallying up a complete column of varying  
values from a text file where you don’t know in advance the what may  
appear in a column etc…

In Ruby, I have worked out a similar the equivalent to be: (with a Hash  
now…)

a={};  
a[“abc”]=a[“abc”].to\_i+1;

But I"m sure there is a shorter way ? Better trick available here?

As an aside to ‘to\_i’ is to turn the ‘nil’ into zero: is this safe to  
assume this?

Thanks - sorry if this is stupid question…

John

---

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [August 8, 2008, 12:15am UTC](https://www.ruby-forum.com/t/most-compact-command-for-associate-array-totalling/145322/2 "2008-08-08T00:15:12Z")

</div>

On Aug 7, 2008, at 4:59 PM, John Pritchard-williams wrote:

> a[“abc”]=a[“abc”].to\_i+1;
> 
> But I"m sure there is a shorter way ? Better trick available here?
> 
> As an aside to ‘to\_i’ is to turn the ‘nil’ into zero: is this safe to  
> assume this?
> 
> Thanks - sorry if this is stupid question…

No stupid questions; they’re all good.

Here’s a minor reworking of what you did to eliminate the #to\_i.

a = Hash.new { |h,k| h[k] = 0 }  
a[“abc”] += 1

The special form of Hash.new that I used above will automatically  
initialize the bucket to 0 for any new key it receives, so you avoid  
the problem with nil and #to\_i.

cr

---

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [August 8, 2008, 12:28am UTC](https://www.ruby-forum.com/t/most-compact-command-for-associate-array-totalling/145322/3 "2008-08-08T00:28:53Z")

</div>

Chuck R. wrote:

> On Aug 7, 2008, at 4:59 PM, John Pritchard-williams wrote:
> 
> > a[“abc”]=a[“abc”].to\_i+1;
> > 
> > But I"m sure there is a shorter way ? Better trick available here?
> > 
> > As an aside to ‘to\_i’ is to turn the ‘nil’ into zero: is this safe to  
> > assume this?
> > 
> > Thanks - sorry if this is stupid question…
> 
> No stupid questions; they’re all good.
> 
> Here’s a minor reworking of what you did to eliminate the #to\_i.
> 
> a = Hash.new { |h,k| h[k] = 0 }  
> a[“abc”] += 1
> 
> The special form of Hash.new that I used above will automatically  
> initialize the bucket to 0 for any new key it receives, so you avoid  
> the problem with nil and #to\_i.
> 
> cr

a = Hash.new(0)  
a[“abc”] += 1

Has the same effect.

Regards,

Siep

---

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [August 8, 2008, 3:38am UTC](https://www.ruby-forum.com/t/most-compact-command-for-associate-array-totalling/145322/4 "2008-08-08T03:38:49Z")

</div>

On Aug 7, 2008, at 5:27 PM, Siep K. wrote:

> > > to
> > 
> > The special form of Hash.new that I used above will automatically  
> > initialize the bucket to 0 for any new key it receives, so you avoid  
> > the problem with nil and #to\_i.
> > 
> > cr
> 
> a = Hash.new(0)  
> a[“abc”] += 1
> 
> Has the same effect.

Quite true, but I find the block form to be a better “general case”  
form to know since it is so much more flexible. But that’s just my  
preference.

cr

---

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [August 8, 2008, 9:47am UTC](https://www.ruby-forum.com/t/most-compact-command-for-associate-array-totalling/145322/5 "2008-08-08T09:47:49Z")

</div>

2008/8/8 Chuck R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> On Aug 7, 2008, at 5:27 PM, Siep K. wrote:
> 
> > Chuck R. wrote:
> > 
> > > On Aug 7, 2008, at 4:59 PM, John Pritchard-williams wrote:

> > > a = Hash.new { |h,k| h[k] = 0 }  
> > > a[“abc”] += 1

> > a = Hash.new(0)  
> > a[“abc”] += 1
> > 
> > Has the same effect.
> 
> Quite true, but I find the block form to be a better “general case” form to  
> know since it is so much more flexible. But that’s just my preference.

That’s true in a way - although for this particular case I’d prefer  
the solution with the default parameter. This can be slightly more  
efficient at times because it does not insert a value on each query to  
the Hash.

For other frequent cases, I use the block form. This is a typical  
(for me) application:

items = Hash.new {|h,k| h[k] = }  
…  
items[item.category] \<\< item

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [August 8, 2008, 11:33am UTC](https://www.ruby-forum.com/t/most-compact-command-for-associate-array-totalling/145322/6 "2008-08-08T11:33:19Z")

</div>

> Quite true, but I find the block form to be a better “general case”  
> form to know since it is so much more flexible.

I guess you have a point here, but visually I find the shorter solution

a = Hash.new(0)  
a[‘abc’] += 1

more appealing.

---

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [August 8, 2008, 12:02pm UTC](https://www.ruby-forum.com/t/most-compact-command-for-associate-array-totalling/145322/7 "2008-08-08T12:02:02Z")

</div>

From: Marc H. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# I guess you have a point here, but visually I find the

# shorter solution

# a = Hash.new(0)

# a[‘abc’] += 1

# more appealing.

careful, it’s more than a face; the previous block example  
creates/assigns objects when called.

compare this,

irb(main):001:0\> h=Hash.new 0  
=\> {}  
irb(main):002:0\> h[1]  
=\> 0  
irb(main):003:0\> h[2]  
=\> 0  
irb(main):004:0\> h  
=\> {}

w this,

irb(main):005:0\> h=Hash.new{|h,k| h[k]=0}  
=\> {}  
irb(main):006:0\> h[1]  
=\> 0  
irb(main):007:0\> h[2]  
=\> 0  
irb(main):008:0\> h  
=\> {1=\>0, 2=\>0}

and this,

irb(main):011:0\* h=Hash.new{|h,k| 0}  
=\> {}  
irb(main):012:0\> h[1]  
=\> 0  
irb(main):013:0\> h[2]  
=\> 0  
irb(main):014:0\> h  
=\> {}

(note that Hash.new{|h,k| 0} may evolve to Hash.new{0} or Hash.new(0);  
ie all of them do not assign/create to the hash)

the common denominator of the above is that they all do the ff (w same  
result)

irb(main):021:0\> h[1] += 1  
=\> 1  
irb(main):022:0\> h  
=\> {1=\>1}

So if you ask me, i find the hash block form very generic.

kind regards -botp (just another block fan 🙂

---

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [August 8, 2008, 5:49pm UTC](https://www.ruby-forum.com/t/most-compact-command-for-associate-array-totalling/145322/8 "2008-08-08T17:49:10Z")

</div>

Thanks for the replies: in fact I also prefer this one:

//  
a = Hash.new(0)  
a[‘abc’] += 1  
//

As it matches exactly the behaviour of the ‘awk’ program and its also  
kinda neat looking.

On the other hand this solution is also really intriguing: ( and I note  
the difference in behaviour as explained earlier in this post):

//  
h=Hash.new{|h,k| h[k]=0}  
//

So can somebody explain in English what is going on here ? This is very  
very Ruby to me 🙂 I’m guessing that we are hanging that block of the  
‘store’ method of ‘h’ from now on ? I mean: so when somebody then calls  
something like:

h.store(mykey,myvalue) it will invoke the block which will result in:

h[mykey]=myvalue - but only if ‘myvalue’ is nil ?

Is this a very specific mechanism to hashes or something really generic?

Thanks again !

John

---

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [August 9, 2008, 7:12am UTC](https://www.ruby-forum.com/t/most-compact-command-for-associate-array-totalling/145322/9 "2008-08-09T07:12:16Z")

</div>

From: John Pritchard-williams [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# So can somebody explain in English what is going on here ?

# This is very very Ruby to me 🙂 I’m guessing that we are

# hanging that block of the ‘store’ method of ‘h’ from now on ?

# I mean: so when somebody then calls something like:

# 

# h.store(mykey,myvalue) it will invoke the block which will result in:

# h[mykey]=myvalue - but only if ‘myvalue’ is nil ?

no. nil is a value and could be value for a hash key.

try

h[mykey] = nil

the block form will invoke the block if the hash key does not exist

try

h.has\_key? mykey

# Is this a very specific mechanism to hashes or something

# really generic?

imho, if you work on hashes, you should know block form of Hash.new. See  
ruby-doc for examples.

kind regards -botp

---

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [August 9, 2008, 2:04pm UTC](https://www.ruby-forum.com/t/most-compact-command-for-associate-array-totalling/145322/10 "2008-08-09T14:04:23Z")

</div>

> # Is this a very specific mechanism to hashes or something
> 
> # really generic?
> 
> imho, if you work on hashes, you should know block form of Hash.new. See  
> ruby-doc for examples.

Thanks ! Will check it out

Cheers

John
