Bryan R Harris <bryan_r_har...@raytheon.com> writes:

>> 
>>    I have code that looks like this:
>> 
>>    **************************************
>>    if ($props =~ /\S/) {
>>        %{$ptr[-1]->[-1]} = ($props =~ m/\s*([^=]+)="([^"]+)"/g);
>> 
>> where is @ptr set? what are you using it for?
>
> Earlier, of course.  Probably no matter for this discussion.
>
>
>>   BRH> }
>> 
>>   BRH> My problem is that I only want to append the properties and their
>>   BRH> values to that hash, not replace that hash with the new
>>   BRH> properties.
>> 
>>   BRH> Is there a notation that will let me do that without requiring
>>   BRH> temporary variables?
>> 
>> given a list of key/value pairs, there is no direct way to append to a
>> hash.
>
> That's what I needed to know.  It'd be nice if someday perl allowed
> something like "push %a, me => 10, you => 11;".

Uri will want to scalp me for commenting on something a good bit over
my head... but just looking at your push notion.  

I think it might actaully be possible to do.

I don't recognize your notation %{$ptr[-1]->[-1]} but if its really
nothing more than a key/value pair.  Could you possible get at it by
inverting the hash (not reversing) with the snippet of code posted
here a while back by Shawn C and included below.  

Then the pairs are reversed but more importantly they become
accessable as:

  @( $inv_hash{ $key } }

Which is then an array.  Or at least acts like an array in some
circumstances. 

Any values (now keys in the inverted hash) that are duplicates do NOT
get swallowed as they would by reversing but instead become available
as the array like above.

Could you then push (append) something on that array, and possibly
reinvert it?  

I would have tested that idea but not sure what kind of critter
`%{$ptr[-1]->[-1]}' might be.

-------        ---------       ---=---       ---------      --------
inversion code:

my  %inv_hash = invert(\%hash);

sub invert {
   my $h = shift @_;
   my %inv = ();

   while( my ( $k, $v ) = each %{ $h } ){
     push @{ $inv{$v} }, $k;
   }
   return %inv;
}


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to