Hashes

T

Tim Wolak

Can Ruby create a hash and have a key and the value be an array?

I am taking account information from a txt file like such:
000123456778900000000-1234567890000000000000000000230000000

I=B9m getting the account number and balance from each line. If the account
numbers are the same I need to add the balances together else move on to the
next. The best way for me to do that would be to store the account number
in a hash key and store the balances from the lines that have the same
account number into an array to be added together later. The code to start
gathering the info is located below. As I am extremely new to Ruby any help
would be appreciated. Thanks in advance.

class Info
attr_reader :acct, :money
=20=20
def initialize(filename)
@acct =3D File.new(filename, "r")
end
f =3D Info.new("Balances20080415.txt")
act =3D f.acct
act.each do |list|
#covert me to a string
futbal =3D list.to_s
#Pull accounts
office =3D futbal[22..24]
if office =3D=3D "RPT"
next
else=20=20
acctnum =3D futbal[24..28]
end
#Pull Liquidating values
lv =3D futbal[217..231]
#Pull LV Indicator
lvind =3D futbal[215..215]
#if Negitave vlaues
if lvind =3D=3D "-"
lvnegfloat =3D lv.to_f/1000
print acctnum," ",lvind, lvnegfloat, "\n"
#else Positive Values
else
lvposflt =3D lv.to_f/1000
print acctnum, " ", lvposflt, "\n"
end
end
end

--=20
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
 
D

David A. Black

Hi --

Can Ruby create a hash and have a key and the value be an array?

Let's ask it :)

$ irb
irb(main):001:0> hash = { [1,2,3] => [4,5,6] }
=> {[1, 2, 3]=>[4, 5, 6]}

That would be a yes :)

I haven't quite analyzed your code sample but you can definitely do
the above.


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.rubypal.com for details and updates!
 
T

Todd Benson

Hi --


Can Ruby create a hash and have a key and the value be an array?

Let's ask it :)

$ irb
irb(main):001:0> hash = { [1,2,3] => [4,5,6] }
=> {[1, 2, 3]=>[4, 5, 6]}

That would be a yes :)

I haven't quite analyzed your code sample but you can definitely do
the above.


David

I didn't realize this before, but even if it's off topic, I thought
I'd bring it up.

Hashes -- during creation -- allow assignment more than once in 1.8.6
with the last one taking precedence

Is that somehow common knowledge that I missed in the Pickaxe? Just
one of those little things, I guess.

irb(main):002:0> h = {[1, 2, 3] => [4, 5, 6], [1, 2, 3] => [7, 8, 9]}
=> {[1, 2, 3]=>[7, 8, 9]}

Todd
 
T

Todd Benson

I didn't realize this before, but even if it's off topic, I thought
I'd bring it up.

Hashes -- during creation -- allow assignment more than once in 1.8.6
with the last one taking precedence

Is that somehow common knowledge that I missed in the Pickaxe? Just
one of those little things, I guess.

irb(main):002:0> h = {[1, 2, 3] => [4, 5, 6], [1, 2, 3] => [7, 8, 9]}
=> {[1, 2, 3]=>[7, 8, 9]}

Todd
Ouch, I guess that would be a bad thing... If I'm going line by line then I
would think I would be ok then as the hash is closed after I finish to take
on the next line correct?

Hmmm nice catch Todd!

Hi Tim,

I didn't have a close look at your original post, but in all honesty
you should probably listen to David. I just brought up a surprise in
Hash creation that I didn't expect. I don't think it would affect
your code.

Todd
 
S

Simon Krahnke

* Todd Benson said:
Hashes -- during creation -- allow assignment more than once in 1.8.6
with the last one taking precedence

I guess that

a = { 1 => 'a', 2 => 'b', 1 => 'c' }

does the same as

a = Hash.new
a[1] = 'a'
a[2] = 'b'
a[1] = 'c'

mfg, simon .... l
 
R

Robert Dober

As David has shown you it is perfectly possible, but (although strings
are used quite often instead of symbols) I would be quite careful when
using mutable objects as hash keys. Look at this very simple example.
554/54 > cat hashkeys.rb && echo "--->" && ruby hashkeys.rb
h = {}

k = [1,2,3]
h[k]=[*1..3]

p h
k.pop
p h
p h[k]
--->
{[1, 2, 3]=>[1, 2, 3]}
{[1, 2]=>[1, 2, 3]}
nil

No big deal in the context but in a complex application you might get
bitten fast.
I would at least consider freezing the objects used as keys.



HTH
Robert
 
D

David A. Black

Hi --

As David has shown you it is perfectly possible, but (although strings
are used quite often instead of symbols) I would be quite careful when
using mutable objects as hash keys. Look at this very simple example.
554/54 > cat hashkeys.rb && echo "--->" && ruby hashkeys.rb
h = {}

k = [1,2,3]
h[k]=[*1..3]

p h
k.pop
p h
p h[k]
--->
{[1, 2, 3]=>[1, 2, 3]}
{[1, 2]=>[1, 2, 3]}
nil

No big deal in the context but in a complex application you might get
bitten fast.
I would at least consider freezing the objects used as keys.

String objects will get copied and frozen automatically:
str = "abc" => "abc"
h = {} => {}
h[str] = 1 => 1
h => {"abc"=>1}
h.keys[0].equal?(str) => false
h.keys[0] << "def"
TypeError: can't modify frozen string
from (irb):6:in `<<'
from (irb):6
from :0=> {"abc"=>1}

Also, for mutable objects you can use rehash:
a = [1,2,3] => [1, 2, 3]
h[a] = 2 => 2
a << 4 => [1, 2, 3, 4]
h[a] => nil
h.rehash => {[1, 2, 3, 4]=>2}
h[a]
=> 2

That's not to say you might not want to freeze things sometimes, but
you can use rehash if you want the object to remain mutable.


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.rubypal.com for details and updates!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Hash keys 3
Negative nums 1
n00b question 4
changing hash key 5
two rails 0.14.1 problems in win 1
inserting hash data into email 0
MiniQuiz : Renesting Nodes (OWLScratch) 1
Fun with Permutations 3

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top