hiddenhippo
1/15/2008 3:17:00 PM
On Jan 15, 1:04 pm, Andrew Timberlake <and...@andrewtimberlake.com>
wrote:
> > hopefully someone might be able to shed some light on this.
> > loop through the rows
> > does the hash contain a key value as read from that row
> > if it does then place the row into an array and then add it to an
> > array of rows which might already be within the hash
> > if the hash doesn't contain the key then create a key and add the
> > row to an array an append it to the hash
>
> I assume that you can already do the database lookup etc.
> If you are then iterating over the rows, try the following:
>
> h = Hash.new {|hash,key| hash[key] = []}
> while(row = ...) #collect each row
> key = row[0]
> h[key] << row
> end
>
> This creates a hash where the value of a newly created entry is always an
> array.
> Therefore, you no longer need to check if the hash has the key or not, you
> can simply append to the array based on the key.
>
> Andrew Timberlake
> and...@andrewtimberlake.com
> 082 415 8283
> skype: andrewtimberlake
>
> "I have never let my schooling interfere with my education."
> --Mark Twain
thanks very much for that - i'll give it go