Ara.T.Howard wrote:

> On Wed, 12 Oct 2005, Kev Jackson wrote:
>
>> I got the output I wanted with this
>>
>> work_types = Hash.new
>> if work_types.has_key?(nsc_id) then
>>   work_types[nsc_id]= work_types[nsc_id].include?(work_type) ? 
>> work_types[nsc_id] : work_types[nsc_id] << work_type
>> else
>>   work_types[nsc_id]= [work_type]
>> end
>>
>> So the problem is solved, but I wonder if there's a more elegant way of
>> doing it (especially the check to see if the value is already in the 
>> array).
>> My first assumption was that assignment to a Hash took a block (hence 
>> the
>> pseudo code), I was actually a little suprised that it didn't ;)
>
>
> this is one easy way
>
>   work_types = Hash::new{|h,k| h[k] = []}
>
>   work_types[ nsc_id ].push( work_type ).uniq!
>
That looks promising - I see it essentially relies on defining Hash and 
setting the initial values, so that I can avoid the  "else  
work_types[nsc_id]= [work_type] end" part

uniq! certainly looks like it would shorten my code.

>
> but does a bit of extra work.  another way would be to use set
>
>   require 'set'
>
>   work_types = Hash::new{|h,k| h[k] = Set::new}
>
>   work_types[ nsc_id ] << work_type
>
Yeah I was thinking of Set, but I want to require/include as little as 
possible to keep complexity down for other people to maintain.

> but you must understand set and it's notion of equality.  plus you 
> lose data
> order but, since you are ignoring dups, i guess this isn't important.
>
> or perhaps you can model your data with a nested hash?
>
>   work_types = Hash::new{|h,k| h[k] = {}}
>
>   work_types[ nsc_id ][ work_type ] = true
>
> and then use
>
>   values = work_types[ nsc_id ].keys
>
> or just make your own apprach more compact
>
>   work_types = Hash::new
>
>   work_types[ nsc_id ] = [ work_types[ nsc_id ], work_type ].compact.uniq
>
I'm not how this works in a manner that's similar to the (verbose but 
easy to understand) version I have already. In fact I can't understand 
half of what's going on here!

> you have options - and there is always sqlite if you start to feel 
> like you
> are rolling query logic on top of this data structure ;-)

Dear god no! :). I'm only munging data dumps from Oracle, I'd hate to 
have to store them in a database just to transform them!

Kev