>>>>>> "BRH" == Bryan R Harris <bryan_r_har...@raytheon.com> writes:
> 
>   BRH> I have code that looks like this:
> 
>   BRH> **************************************
>   BRH> if ($props =~ /\S/) {
>   BRH>     %{$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;".


> and append is a poor term anyhow. there are modules on cpan with
> various subs that do this. here is one way to do it with no temp vars:
> this does copy the old hash and the new elements are added which will
> overwrite any old keys if there are dups. just reorder the two parts to
> make the old hash's values overwrite the new ones.
> 
> %hash = ( %hash, ($props =~ m/\s*([^=]+)="([^"]+)"/g) ) ;
> %hash = ( ($props =~ m/\s*([^=]+)="([^"]+)"/g), %hash ) ;
> 
> another way without copying the hash is to loop over the new keys/values
> with each on an anon hash:
> 
> while( my( $key, $value ) =
> each( %{ $props =~ m/\s*([^=]+)="([^"]+)"/g } ) ) {
> 
> $hash{ $key } = $value ;
> }
> 
> you can also loop over the list 2 at a time with different tricks and do
> the same assignment. there are zip functions and others on cpan that
> will take 2 elements at a time from a list. they do use temp vars inside
> so they can keep track of the index as you can't do that with a list
> itself.

Thanks, Uri, you've been a big help!

- Bryan




-- 
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