Robert Klemme <shortcutter / googlemail.com> wrote:

> 2008/1/24, Michael Schmarck <michael.schmarck / here.la>:
>> Hello.
>>
>> I already asked about how I'd best re-order a datastructure. But
>> thinking about, I changed my mind; instead of the hash approach
>> I've shown in <news:6233013.YDsyxgeUJ1 / michael-schmarck.my-fqdn.de>,
>> I now think I might use arrays instead. Reason: The order in which
>> something is inserted and later fetched is important to me. With
>> Hashes in 1.8, this cannot easily be done out-of-the-box, can it?
>>
>> Anyway - I'm now looking for a way to change the following arry:
> 
> This is not an Array but an object graph composed of nested Arrays.
> 
>> timing = [
>>       ["Performance Test of Item Access using Lists", [
>>           [["Plants", 100], ["Customers", 50], ["Total", 150]],
>>           [["Plants",  85], ["Customers", 60], ["Total", 145]],
>>           [["Plants", 111], ["Customers", 77], ["Total", 188]]
>>       ]],
>>       ["Performance Test of Item Access using Advance Item Search", [
>>           [["Work List", 17], ["Bookmarks", 30], ["Total", 42]],
>>           [["Work List", 10], ["Bookmarks", 33], ["Total", 50]],
>>           [["Work List", 22], ["Bookmarks", 27], ["Total", 99]]
>>       ]]
>> ]
>> # This should become:
>> timing_reordered = [
>>      ["Performance Test of Item Access using Lists", [
>>          ["Plants", [100, 85, 111]], ["Customers", [50, 60, 77]],
>>          [["Total", [150, 145, 188]]
>>      ]],
>>      ["Performance Test of Item Access using Advance Item Search", [
>>          ["Work List", [17, 10, 22]], ["Bookmarks", [30, 33, 27]],
>>          [["Total", [42, 50, 99]]
>>      ]]
>> ]
> 
> My first advice would be to use proper data types, e.g.
> 
> S1 = Struct.new :plants, :customers, :total
> S2 = Struct.new :work_list, :bookmarks, :total

Can I automatically create those elements of the Struct? I
don't want to have to fiddle with the data type, when a new
test comes up. And I'd also like to have easy access to the
name of the test in a well written way (ie. "Work List").
How do I iterate over the elements of the Struct? I absolutely
do not want to have to rely on knowing the names of all these
data points.

I think I'd be all set, if I could use hashes, because then
I'd have all I need - but in Ruby 1.8, the order of the elements
of a hash is not "stable". Ie., I "insert" "Work List", "Bookmarks"
and "Total", but when I iterate over the hash, I get back "Bookmarks",
"Work List" and "Total" :( No good :(

As I don't want to lose the order in which something has been
added to the datastructure, I'm forced to use arrays in Ruby
1.8, aren't I?

[...]
> With the structs above
> 
> # data contains S1
> data.inject(S1.new) do |s,d|
>   (s.plants ||= []) << d.plants
>   (s.customers ||= []) << d.customers
>   (s.total ||= []) << d.total

Urgs. I don't want to have to know that there's a "plants", "customers",
"total", etc.pp..

Thanks a lot,

Michael