[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

hash adding values

Tim Wolak

5/7/2008 5:44:00 PM

I'm trying to insert account numbers into a hash and add the balances
together that are stored in the value. Is there a reason this is not
working?

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

17 Answers

Sebastian Hungerecker

5/7/2008 6:21:00 PM

0

Tim Wolak wrote:
> Is there a reason this is not
> working?
>
> sktylist =3D Hash.new("")
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 sktylist[@acctnum] +=3D [value]
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0p sktylist

Yes, there is: String#+ does not expect an array as an argument.
sktylist[@acctnum] +=3D value would work if value is a string.
(Alternatively you could change the hash to hold arrays instead
of strings and keep the +=3D [value] part as-is, if that's what
you want).


HTH,
Sebastian
=2D-=20
Jabber: sepp2k@jabber.org
ICQ: 205544826

Tim Wolak

5/7/2008 6:25:00 PM

0

Sebastian Hungerecker wrote:
> Tim Wolak wrote:
>> Is there a reason this is not
>> working?
>>
>> sktylist = Hash.new("")
>>             sktylist[@acctnum] += [value]
>>          p sktylist
>
> Yes, there is: String#+ does not expect an array as an argument.
> sktylist[@acctnum] += value would work if value is a string.
> (Alternatively you could change the hash to hold arrays instead
> of strings and keep the += [value] part as-is, if that's what
> you want).
>
>
> HTH,
> Sebastian

So would it be easier converting it to a string or have the hash hold
arrays? All I need is the value to be an array? Is there an easy way
to do this?

Thanks for the help.
Tim
--
Posted via http://www.ruby-....

Craig Demyanovich

5/7/2008 6:28:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Wed, May 7, 2008 at 1:43 PM, Tim Wolak <tim.wolak@gmail.com> wrote:

> I'm trying to insert account numbers into a hash and add the balances
> together that are stored in the value. Is there a reason this is not
> working?
>
> sktylist = Hash.new("")
> sktylist[@acctnum] += [value]
> p sktylist


According to docs for Hash.new, you're initializing the Hash and telling it
that the default value for any new elements is an empty string. Then, you
might be accessing the hash with a key that doesn't exist, so you're getting
that default value, the empty string. Then you're trying to concatenate an
array to it. I assume that you don't want to do that based on your problem
description. Maybe you want to do something like this (from an IRB session):

$ irb
>> h = Hash.new(0.0)
=> {}
>> h["a"] += 2.2
=> 2.2
>> h["a"] += 3
=> 5.2
>> h
=> {"a"=>5.2}

Regards,
Craig

Craig Demyanovich

5/7/2008 6:29:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

Oh, you want to store the balances based on the acct. #, then you want to
iterate the balances later and sum them?

Tim Wolak

5/7/2008 6:35:00 PM

0

Craig Demyanovich wrote:
> Oh, you want to store the balances based on the acct. #, then you want
> to
> iterate the balances later and sum them?

Yes, I need to take the accounts that have the same number and leave the
key that same number and sum the balances that are stored in the value
for that account. Then I can iterate over the hash and print the account
nums and balances in an email.

Thanks,
Tim
--
Posted via http://www.ruby-....

Craig Demyanovich

5/7/2008 6:37:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

Something like this?

>> h = Hash.new( [] )
=> {}
>> h["a"] << 2.2
=> [2.2]
>> h["a"] << 3
=> [2.2, 3]
>> sum_of_a = 0.0
=> 0.0
>> h["a"].inject { |sum_of_a, value| sum_of_a += value }
=> 5.2
>> sum_of_a
=> 5.2

I have no idea if that's the best way to do that; I'm still earning my Ruby
chops.

Regards,
Craig

Tim Wolak

5/7/2008 6:42:00 PM

0

Craig Demyanovich wrote:
> Something like this?
>
>>> h = Hash.new( [] )
> => {}
>>> h["a"] << 2.2
> => [2.2]
>>> h["a"] << 3
> => [2.2, 3]
>>> sum_of_a = 0.0
> => 0.0
>>> h["a"].inject { |sum_of_a, value| sum_of_a += value }
> => 5.2
>>> sum_of_a
> => 5.2
>
> I have no idea if that's the best way to do that; I'm still earning my
> Ruby
> chops.
>
> Regards,
> Craig

I'm pretty new to ruby as well. I have the following and its saying
"can't conver float into a string". Keep in mind that the accounts and
balances are stored in variables.

class Info
attr_reader :acct, :money

def initialize(filename)
@acct = File.new(filename, "r")
end


f = Info.new("SKTYFutBalances20080415.txt")
act = f.acct
act.each do |list|
#covert me to a string
futbal = list.to_s
#Pull accounts
office = futbal[21..23]
if office == "RPT"
next
else
@acctnum = futbal[24..28]
end
#Pull Liquidating values
@lv = futbal[217..230]
#Pull LV Indicator
is_negative = futbal[215..215] == '-'
value = @lv.to_f/100
value = -value if is_negative
#puts @acctnum,value
#test = @acctnum.to_sym
sktylist = Hash.new("")
sktylist[ @acctnum ] << value

p sktylist
end

end
--
Posted via http://www.ruby-....

Craig Demyanovich

5/7/2008 6:43:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

I posted some code just before you replied. Did it help?

Tim Wolak

5/7/2008 6:49:00 PM

0

Craig Demyanovich wrote:
> I posted some code just before you replied. Did it help?

I did not store anything when I run it with my code I posted I get:


{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
--
Posted via http://www.ruby-....

Craig Demyanovich

5/7/2008 6:49:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

Looks like you forgot to change this line

sktylist = Hash.new("")

to

sktylist = Hash.new( [] )

which makes the default value for new elements an empty array instead of a
string. Only then can you use the << operator to push values on to the
array.

Regards,
Craig