Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
I have a record structure like this
my %hash = ( 'this_type' => { 'field1' => { REQ => 'N', POS => 34, LENGTH => 10, data => ' ' x 10, FILTER => \&default_filter() } }, 'that_type' => { 'field1' => { REQ => 'Y', POS => 34, LENGTH => 10, data => ' ' x 10, FILTER => \&default_filter() }, 'field2' => { REQ => 'Y', POS => 35, LENGTH => 10, data => ' ' x 10, FILTER => \&default_filter() }, }, # and a lot more records... );
Now, the question is how to assign data to this...
This does not work:
@$hash{$type}->{@sorted_keys}->{data} = @mydata;
Should I simply assign like this:
for my $k (@sorted_keys) { $hash{$type}->{$k}->{data} = shift @mydata; }
L

Replies are listed 'Best First'.
Re: hash assignment in complex structure
by borisz (Canon) on Apr 17, 2005 at 18:18 UTC
    Your second way is fine. The first did not work, since the slice must be at the end.
    Boris
Re: hash assignment in complex structure
by tlm (Prior) on Apr 17, 2005 at 18:26 UTC

    Update: Upon re-reading your post I now realize that you asked something much simpler than what I answered (basically I misinterpreted the word "data" in your question). borisz is right; your second approach will work, though you can save yourself a bit of typing by omitting the -> 's:

    for my $k (@sorted_keys) { $hash{$type}{$k}{data} = shift @mydata; }
    I still think my original answer may be of some use to you.

    the lowliest monk

Re: hash assignment in complex structure
by davidrw (Prior) on Apr 17, 2005 at 18:15 UTC
    Hm. I don't know enough about slices to know if it's possible like that -- Seems like loop might be the way to go (and maybe clearer anyways. Here's a slightly different loop syntax (also, this is non-destructive):
    $hash{$type}->{ $sorted_keys[$_] }->{data} = $mydata[$_] for 0 .. $#so +rted_keys;