in reply to Expanding / flattening a structure

20050824 => { to => 'new york', to => 'london', },
You cannot have a hash that has the same key twice.


holli, /regexed monk/

Replies are listed 'Best First'.
Re^2: Expanding / flattening a structure
by jaa (Friar) on Aug 24, 2006 at 18:36 UTC
    Apologies, you are right, here is a better example of the input:
    my $struct = { fruit => [qw( apple pear )], type => [qw( farmed organic )], period => { 20050824 => { to => ['new york', 'london', ], }, 20050825 => { to => ['auckland', ], }, } };
    update: fixed first date [ -> {
      Maybe:
      # example structure my $struct = { fruit => [qw( apple pear )], type => [qw( farmed organic )], period => { 20050824 => [ to => ['new york', 'london' ], ], 20050825 => [ to => ['auckland' ], ], }, };
      Would be syntactically valid?

      But given the only attribute to the date is to, why not remove that layer?

      jdtoronto

        two reasons for the 'to':

        1) the token 'to' names the values in the result,

        e.g. I end up with this in the expanded array of hashes:

        to => 'auckland',

        2) also, this is just an example, in reality there are more values, e.g.

        period => { 20050824 => { to => [ 'auckland', 'new york', ], transport => [ 'air', ] }, },
        I think that $struct is already reasonable efficient? Did you have a recommendation for a better way to structure it?