Could we see some code? There are multiple things that could have gone wrong. If you have not already, please use strict; in your script and declare all variables with local scope.
my %hash = ('key1' => 'v1', 'key2' => 'v2');
del_hash_key(\%hash);
sub del_hash_key {
my $params = shift;
my %hash = %$params;
delete $hash{'key1'};
print %hash; # Prints the latter key/value pair
print "\n";
}
print %hash; # Prints both key/value pairs
I'm so adjective, I verb nouns! chomp; # nom nom nom
| [reply] [d/l] [select] |
Lawliet's method (copying the hash) is the only way I can think of meeting your requirements. However, if you'll settle for keeping the key while having its value undefined, you can avoid copying the hash.
my %hash = ('key1' => 'v1', 'key2' => 'v2');
del_hash_key(\%hash);
sub del_hash_key {
my $hash = shift;
local $hash->{'key1'} = undef; # Until end of scope.
print %$hash; # Prints the latter key/value pair
print "\n";
}
print %hash; # Prints both key/value pairs
| [reply] [d/l] |
my %hash = ( 1 .. 4);
my $foo = \%hash;
my $bar = \%hash;
my $baz = $foo ;
foobarbaz(\%hash);
sub foobarbaz {
my $ref = shift;
}
Many names (references) for it ($foo,$bar,$baz) but just one %hash, it doesn't matter where you modify it (even inside sub foobarbaz).
I suggest you switch to a book like Beginning Perl by Simon Cozens, Peter Wainwright.
Beginning Perl is a different kind of Perl book. It's written particularly with the beginning programmer in mind, but it doesn't treat you like an idiot, and experienced programmers will not feel patronised. It covers a lot of ground, from the very basics of programming, right through to developing CGI applications for the web. More importantly, it emphasises good Perl practice, and readable and maintainable code. | [reply] [d/l] |