Damiii
Damiii

Reputation: 1373

How to delete a key from a referenced hash?

I would like to know how can I delete a key from a referenced hash ? I tried some example which I see on internet but none is working...

$dicA->{$keysA} = "\$";
delete($dicA{$keysA});

That method isn't working and it's giving me that error (which I don't know)

Error: Global symbol "%dic" requires explicit package name at /Users/.../PrefixTree.pm line 217.
(line 217 -> delete line)

I would like to have some advice please!

Upvotes: 1

Views: 98

Answers (2)

Birei
Birei

Reputation: 36252

You have some problems.

First, use the strict and warnings pragmas.

Second, declare variables before using them:

my ($dicA, $keysA);

Third, $dicA->{} and $dicA{} are different variables. The first one is a scalar reference, and the second one is a hash. Use the same one:

#!/usr/bin/env perl

use warnings;
use strict;

my $dicA;
my $keysA = 'key';

$dicA->{$keysA} = "\$";
delete($dicA->{$keysA});

Upvotes: 2

Elliott Frisch
Elliott Frisch

Reputation: 201399

Like so delete $dicA->{$keysA}; or in a more complete example

#!/usr/bin/env perl

$keysA='Hello';
$dicA = {};
$dicA->{$keysA} = "\$";

print "$dicA\n";
print "key \"$dicA->{$keysA}\"\n";
delete $dicA->{$keysA};
print "$dicA\n";
print "key \"$dicA->{$keysA}\"\n";

Upvotes: 2

Related Questions