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

I am trying to pass 2 file handles AND 2 hashes by reference and I think I did it correctly but I dont know how to get the values once I am in the sub "catch_error". Can someone offer guidance?
my $location_hash = get_location(\*ZIPS); my $cat_hash = get_categories (\*SICS); for my $city ( keys %$location_hash ) { printf "%s %s %s\n", $city, $location_hash->{ $city }->{ 'ST' }, $l +ocation_hash->{ $city }->{ 'ZIP' } ; for my $cat ( keys %$cat_hash) { printf "%s %s\n", $cat, $cat_hash->{ $cat }->{ 'NUM' } ; . . . catch_error(\*LOGS, \$page, \$location_hash, \$cat_hash ); # +how can I pass by ref ? # I cant seem to delete the keys that were used. So so I wi +ll set a value # delete ($location_hash->{ $city }->{ 'ST' }) ; #### Addi +ng another post about this because I cant remove the key $location_hash->{ $city }->{ 'PR' } = 1; $cat_hash->{ $cat }->{ 'PR' } = 1; } # for my $cat ( keys %$cat_hash) } # for my $city ( keys %$location_hash ) { sub catch_error{ my ($fh, $str, % ) = @_; # confused right here! ### Then I will print to the log and I will print the keys ### and values from the hashes that I did not process. ### I am having a problem deleting the key so I added a key and set + it to 1 ### I will create another posting for that question titled "How can + I delete a key from a hash". }

Replies are listed 'Best First'.
Re: Function hash by reference.
by BrowserUk (Patriarch) on Jan 07, 2007 at 23:06 UTC

    This my $location_hash = get_location(\*ZIPS);

    and this my $cat_hash = get_categories (\*SICS); are already a hash references, so you don't need to use backslashes

    here catch_error(\*LOGS, \$page, $location_hash, $cat_hash );.

    Then, you just name the references inside the sub and use them in exactly the same way as you have in the code outside the sub.

    sub catch_error{ my ($fh, $str, $location_hash, $cat_hash ) = @_; ... delete $location_hash->{'the key'}; ... }

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Thank you very much!
Re: Function hash by reference.
by shigetsu (Hermit) on Jan 07, 2007 at 23:06 UTC
    Pass $location_hash without a leading backslash, because with a leading backslash, it will generate a reference to a hash reference. Once you're in catch_error() dereference the hash as following: %$location_href.

    You should actually never pass an array or hash directly to a subroutine if you have multiple arguments being passed to it.

      Thank you!