K
Kev Jackson
Hi all,
I'm trying to loop across a dataset and create a hash where each value
is an array so that later I can loop over the hash and for each key
(it's important I store the key), I can loop over the array contained
and spit out some results.
Without looking at the docs I wanted to do something like...
(in pseudo-code)
loop across data here
work_types[nsc_id]=do |types|
types << data[7]
end
end loop
where work_types is the hash and types is the array I want to accumulate
data in
This doesn't work, so I'm wondering what the ruby idiom for this kind of
thing would be. Essentially for each piece of data I want to get the
appropriate value from the Hash and append the value on to the end of
the array associated with the key, or if it doesn't exist in the Hash,
create a new entry with a new array populated with the value.
I'm sure there's a very simple way of doing this, but I can't see the
method I want in the standard library docs - I thought it might be
collect, but it doesn't look like it
Thanks
Kev
I'm trying to loop across a dataset and create a hash where each value
is an array so that later I can loop over the hash and for each key
(it's important I store the key), I can loop over the array contained
and spit out some results.
Without looking at the docs I wanted to do something like...
(in pseudo-code)
loop across data here
work_types[nsc_id]=do |types|
types << data[7]
end
end loop
where work_types is the hash and types is the array I want to accumulate
data in
This doesn't work, so I'm wondering what the ruby idiom for this kind of
thing would be. Essentially for each piece of data I want to get the
appropriate value from the Hash and append the value on to the end of
the array associated with the key, or if it doesn't exist in the Hash,
create a new entry with a new array populated with the value.
I'm sure there's a very simple way of doing this, but I can't see the
method I want in the standard library docs - I thought it might be
collect, but it doesn't look like it
Thanks
Kev