PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Arrays
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Arrays

Réponse
 
LinkBack Outils de la discussion
Vieux 06/06/2008, 23h11   #1
Justin To
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Arrays

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!!!!!
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 07/06/2008, 00h03   #2
Kevin Ballard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Arrays

I've replied below.

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

> 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??


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 Ballard

--
Kevin Ballard
http://kevin.sb.org
kevin@sb.org
http://www.tildesoft.com



  Réponse avec citation
Vieux 07/06/2008, 00h54   #3
Justin To
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Arrays

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


??



--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h57.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,08943 seconds with 11 queries