# Hash of Arrays nItems problem

**URL:** <https://www.ruby-forum.com/t/hash-of-arrays-nitems-problem/83449>\
**Category:** Ruby\
**Created:** [March 17, 2006, 11:15pm UTC](https://www.ruby-forum.com/t/hash-of-arrays-nitems-problem/83449 "2006-03-17T23:15:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 17, 2006, 11:15pm UTC](https://www.ruby-forum.com/t/hash-of-arrays-nitems-problem/83449/1 "2006-03-17T23:15:58Z")

</div>

I’m having a bit of problem using a hash of arrays. It appears that  
nitems are wrong for the individual elements are wrong when I do this:

#!/usr/bin/env ruby  
$:.unshift(File.join(‘…’, ‘lib’))  
require ‘http-access2’

h = HTTPAccess2::Client.new()  
element = Hash.new([])

while urlstr = ARGV.shift  
response = h.get(urlstr) { |data|

data.gsub(/\<(description|title|link)\>(.+)\</(description|title|link)\>/)  
{  
print $1,“\n”  
element[$1].push($2.gsub(/\s\s+/m," "))  
}  
}  
end

print "site description —\> ", site\_description, “\n”  
print "site title ---------\> ", site\_title, “\n”  
print "description.nitems → ", element[‘description’].nitems, “\n”  
print "title.nitems -------\> ", element[‘title’].nitems, “\n”

site\_description = element[‘description’].shift  
site\_title = element[‘title’].shift

while element[‘description’].nitems \> 0  
print element[‘title’].shift.gsub(/\n+/, “”),“\n”  
print element[‘description’].shift.gsub(/\n+/, “”),“\n\n”  
end

% ruby rss.rb [http://involution.com/rss.php](http://involution.com/rss.php)  
site description —\> [involution.com](http://involution.com)  
site title ---------\> [http://involution.com](http://involution.com)  
description.nitems → 30  
title.nitems -------\> 30

There are 10 of each links, putting a print in the hget enclosure shows  
that only 10 descriptions, links, and titles are added, but the  
individual array sizes are reported as 30. What am I doing wrong?

Tony

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 19, 2006, 1:34pm UTC](https://www.ruby-forum.com/t/hash-of-arrays-nitems-problem/83449/2 "2006-03-19T13:34:01Z")

</div>

removed\_email\_address@domain.invalid wrote:

> I’m having a bit of problem using a hash of arrays. It appears that  
> nitems are wrong for the individual elements are wrong when I do this:
> 
> #!/usr/bin/env ruby  
> $:.unshift(File.join(’…’, ‘lib’))  
> require ‘http-access2’
> 
> h = HTTPAccess2::Client.new()  
> element = Hash.new([])

You want

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

Otherwise you’ll be adding to the _same_ single array instance for _all_  
elements.

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 19, 2006, 11:58pm UTC](https://www.ruby-forum.com/t/hash-of-arrays-nitems-problem/83449/3 "2006-03-19T23:58:56Z")

</div>

Oh, snap. Thanks robert. I will name my first son Robert in your  
honor. Thanks a million.

Tony P.  
[http://involution.com](http://involution.com)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 20, 2006, 10:43am UTC](https://www.ruby-forum.com/t/hash-of-arrays-nitems-problem/83449/4 "2006-03-20T10:43:37Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Oh, snap. Thanks robert. I will name my first son Robert in your  
> honor. Thanks a million.

You’re welcome. And I feel humbled.

Kind regards

```
robert
```
