[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

changing hash key

Tim Wolak

5/7/2008 1:04:00 PM

I am working with a script to record accounts and their balances. I
need to check the key in a hash and if the account number is already
there just add the blanace to the value. So far this is what I have and
its not quite doing what I need. Is my test not right? I get all the
account numbers and their balances instead of a list with the accounts
and balances without the duplicate account numbers.

Thanks,
Tim

sktylist = Hash.new("")
if sktylist.has_key?('@acctnum')
sktylist.merge!('@acctnum' => 'value')
else
sktylist[@acctnum] = [value]
end
sktylist.each { |key, value| puts "#{key} equals #{value}"
}
--
Posted via http://www.ruby-....

5 Answers

Sandro Paganotti

5/7/2008 1:22:00 PM

0

This is not working because you're using a string called '@acctnum'
instead of the content of the variable @acctnum inside both has_key? and
merge functions

First remove ' signs from that functions, next I suggest you to use
symbols instead
of variables for keys (@acctnum.to_sym instead of @acctnum)

Bye

sktylist = Hash.new("")
if sktylist.has_key?('@acctnum')
sktylist.merge!('@acctnum' => 'value')
else
sktylist[@acctnum] = [value]
end
sktylist.each { |key, value| puts "#{key} equals #{value}"
}

On Wed, May 7, 2008 at 1:04 PM, Tim Wolak <tim.wolak@gmail.com> wrote:
> I am working with a script to record accounts and their balances. I
> need to check the key in a hash and if the account number is already
> there just add the blanace to the value. So far this is what I have and
> its not quite doing what I need. Is my test not right? I get all the
> account numbers and their balances instead of a list with the accounts
> and balances without the duplicate account numbers.
>
> Thanks,
> Tim
>
> sktylist = Hash.new("")
> if sktylist.has_key?('@acctnum')
> sktylist.merge!('@acctnum' => 'value')
> else
> sktylist[@acctnum] = [value]
> end
> sktylist.each { |key, value| puts "#{key} equals #{value}"
> }
> --
> Posted via http://www.ruby-....
>
>



--
Go outside! The graphics are amazing!

Tim Wolak

5/7/2008 5:01:00 PM

0

Sandro Paganotti wrote:
> This is not working because you're using a string called '@acctnum'
> instead of the content of the variable @acctnum inside both has_key? and
> merge functions
>
> First remove ' signs from that functions, next I suggest you to use
> symbols instead
> of variables for keys (@acctnum.to_sym instead of @acctnum)
>
> Bye
>
> sktylist = Hash.new("")
> if sktylist.has_key?('@acctnum')
> sktylist.merge!('@acctnum' => 'value')
> else
> sktylist[@acctnum] = [value]
> end
> sktylist.each { |key, value| puts "#{key} equals #{value}"
> }

I've created the hash and can store all keys and values however I'm
trying += to add the balances which is value in the hash. So far the
accounts that have the same number are not being consolidated into one
key, value.... Can someone tell me what I'm doing wrong with this?

Thanks,
Tim

sktylist = Hash.new("")
sktylist[@acctnum] += [value]
p sktylist
--
Posted via http://www.ruby-....

Robert Dober

5/8/2008 8:23:00 AM

0

On Wed, May 7, 2008 at 7:00 PM, Tim Wolak <tim.wolak@gmail.com> wrote:
> Sandro Paganotti wrote:
> > This is not working because you're using a string called '@acctnum'
> > instead of the content of the variable @acctnum inside both has_key? and
> > merge functions
> >
> > First remove ' signs from that functions, next I suggest you to use
> > symbols instead
> > of variables for keys (@acctnum.to_sym instead of @acctnum)
> >
> > Bye
> >
> > sktylist = Hash.new("")
> > if sktylist.has_key?('@acctnum')
> > sktylist.merge!('@acctnum' => 'value')
> > else
> > sktylist[@acctnum] = [value]
> > end
> > sktylist.each { |key, value| puts "#{key} equals #{value}"
> > }
>
> I've created the hash and can store all keys and values however I'm
> trying += to add the balances which is value in the hash. So far the
> accounts that have the same number are not being consolidated into one
> key, value.... Can someone tell me what I'm doing wrong with this?
>
>
> Thanks,
> Tim
>
> sktylist = Hash.new("")
> sktylist[@acctnum] += [value]
> p sktylist
> --
>
Given the limited amount of information I have I would guess that you
might doing this

TypeError: can't convert Array into String

wrong.

HTH
Robert



--
http://ruby-smalltalk.blo...

---
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

Damjan Rems

5/8/2008 9:44:00 AM

0


How about:

sktylist = Hash.new
sktylist.[@acctnum] ||= 0
sktylist.[@acctnum] += value


by
TheR
--
Posted via http://www.ruby-....

Sebastian Hungerecker

5/8/2008 2:58:00 PM

0

Damjan Rems wrote:
> =C2=A0 =C2=A0sktylist =3D Hash.new
> =C2=A0 =C2=A0sktylist.[@acctnum] ||=3D 0

sktylist =3D Hash.new(0)


=2D-=20
NP: Depeche Mode - Strangelove
Jabber: sepp2k@jabber.org
ICQ: 205544826