# Arrays

**URL:** <https://www.ruby-forum.com/t/arrays/139787>\
**Category:** Ruby\
**Created:** [June 7, 2008, 12:13am UTC](https://www.ruby-forum.com/t/arrays/139787 "2008-06-07T00:13:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 7, 2008, 12:13am UTC](https://www.ruby-forum.com/t/arrays/139787/1 "2008-06-07T00:13:06Z")

</div>

modArr = Array.new  
freq = Array.new([[0] ])  
values = Array.new([[] ])

#--------------Open the output file and process each line-----  
File.open(‘output.txt’, ‘r’).each { |x|

#-------------Create unique keys according to |x|---------------  
#–Calculate the modulus ([add each digit]%10) and use it as hash  
key–  
key = 0; modF = x.each\_byte { |y| key += y.chr.to\_i }  
#–For slight optimization, change the resulting key into a symbol  
mod = (key%100).to\_s.intern

```
  #--If the key D.N.E. then add it into modArr

  if(!modArr.include?(mod))

    #--Add the value
    values[modArr.size][0] = x.chomp

    #--Add the frequency
    freq[modArr.size][0] += 1

    #--Add the mod
    modArr[modArr.size] = mod

    end

```

}

Hi, I’m still trying to work on the same problem that I posted a few  
days ago… when i run this program, this error reads:

BLANK.rb:22: undefined method `[]=' for nil:NilClass (NoMethodError) from BLANK.rb:7:in`each’  
from BLANK.rb:7

Why is this? And how do I fix it??

Thanks in advance!!!

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 7, 2008, 1:05am UTC](https://www.ruby-forum.com/t/arrays/139787/2 "2008-06-07T01:05:52Z")

</div>

I’ve replied below.

On Jun 6, 2008, at 3:11 PM, Justin To wrote:

> key–  
> values[modArr.size][0] = x.chomp  
> end
> 
> Why is this? And how do I fix it??

You’re using a rather unconventional way of appending to an array,  
i.e. ary[ary.size] = value. This works in the general case. Where it  
doesn’t work is in your code where you say, effectively, ary[ary.size]  
[0] = value. The problem is that ary[ary.size] is returning nil.

-Kevin B.

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 7, 2008, 1:55am UTC](https://www.ruby-forum.com/t/arrays/139787/3 "2008-06-07T01:55:53Z")

</div>

modArr = Array.new  
freq = Array.new([[0] ])  
values = Array.new([[] ])

#--------------Open the output file and process each line-----  
File.open(‘output.txt’, ‘r’).each { |x|

#-------------Create unique keys according to |x|---------------  
#–Calculate the modulus ([add each digit]%10) and use it as hash  
key–  
key = 0; modF = x.each\_byte { |y| key += y.chr.to\_i }  
#–For slight optimization, change the resulting key into a symbol  
mod = (key%100).to\_s.intern

```
  if(!modArr.include?(mod))

    #--Add the value
    values[modArr.size].push(x.chomp)

    #--Add the frequency
    freq[modArr.size].push(1)

    #--Add the mod
    modArr.push(mod)

    end

```

}

BLANK.rb:22: undefined method `push' for nil:NilClass (NoMethodError) from BLANK.rb:7:in`each’  
from BLANK.rb:7

??
