[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] side effect with hash of hashes



Hi,

in a script dealing with the addresses of pet owners I present the user a
search dialog allowing him/her to select one or more owners. This selection
can be looked at and changed in another dialog window. The data of owners in
the selection are kept in a hash of hashes %$rhoh_SelOwner, e.g.:

{ OWN1 => {surname => Ownion,
                       firstname => Chris,
		...}, # other record fields
  BADE1 => {surname => Bader,
		firstname => Rainer,
		...}
}

It has the same structure as the big HoH  %$rhoh_Owner. In the array
@$ra_KeyOwner I keep the names of the record fields.

To safe the changes in the HoH %$rhoh_SelOwner in the big HoH  %$rhoh_Owner
I wrote the routine SafeOwner:

sub SafeOwner {
  my ($dlg) = @_;

  # Put entries from dialog fields back in owner selection
  # in $rhoh_SelOwner:
  my $key = $dlg->item_text(11);
  my $i;
  my $j = 1;
  for ($i = 12; $i < 19; $i++) { # text items in the dialog have item nos.
					    # 12 to 18
    $rhoh_SelOwner->{$key}{$ra_KeyOwner->[$j]} = $dlg->item_text($i);
    $j++;
  } # end for

  # All entries in $rhoh_SelOwner are saved in $rhoh_Owner:
  foreach $key (%$rhoh_SelOwner) {
    my $temphash = $rhoh_SelOwner->{$key};
    $rhoh_Owner->{$key} = $temphash;
  } # end foreach
  
  $OwnerChanged = 1; # keep in mind that changes were made
  # Dispose of dialog via routine "okay":
  return(okay($dlg));
} # end sub SafeOwner

When I dump the HoHs %$rhoh_Owner and %$rhoh_SelOwner I get the following
result:

The entries and structure of %$rhoh_SelOwner are correct. In the HoH
%$rhoh_Owner I get (in addition to the correctly changed entries from the
selection) key-value pairs like

HASH(0x2345fb3) => undef

created by the foreach-loop. The addresse in parentheses point to the
corresponding complete entries.

Is that caused by the syntax of my foreach-loop? Is that "normal"?

At present, my solution is to delete all entries with keys startin with
"HASH(". But is there a way to avoid this?

Thanks for any comments,

Florian

----------------------------------------------------------------------
- Dr. F. Colbatzky          Dept. of Non-clinical Drug Safety        -
- Tel. +49 (7351) 547237    Boehringer Ingelheim Pharma KG, Biberach -
- Fax  +49 (7351) 542183    D-88397 Biberach/Riss                    -


***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch