Undefined array values

  • Thread starter Experienced but Undocumented
  • Start date
E

Experienced but Undocumented

Newbie question: I've got an array like so:
$foo{'bar'} = "test";

Is there a way to assign a default value to any undefined values? For
example, if the program requests $foo{'abc'} and we haven't defined it, for
the default value to be used?

Thanks
 
A

Andreas Kahari

Newbie question: I've got an array like so:
$foo{'bar'} = "test";

That's a hash, but never mind.
Is there a way to assign a default value to any undefined values? For
example, if the program requests $foo{'abc'} and we haven't defined it, for
the default value to be used?

You could do something like this:

$value = (exists $foo{'bar'} ? $foo{'bar'} : $defaultvalue);

where $defaultvalue is some default value. Look up the
documentation for the exist function ("perdoc -f exists") and
the ternary "?:" operator ("perldoc perlop").
 
S

Steve Grazzini

Experienced but Undocumented said:
Newbie question: I've got an array like so:
$foo{'bar'} = "test";

That's usually called a hash (since the "array" is another of
Perl's basic data types, and it's not the one you mean).
Is there a way to assign a default value to any undefined values?
For example, if the program requests $foo{'abc'} and we haven't defined
it, for the default value to be used?

You could do it with TIEHASH --

{
package Hash::Default;
use Tie::Hash;
our @ISA = qw(Tie::ExtraHash);

sub TIEHANDLE {
my ($class, $default) = @_;
bless [{}, $default], $class;
}

sub FETCH {
my ($ref, $default) = @{ shift() };
my $key = shift;

exists $ref->{$key} ? $ref->{$key} : $default;
}
}

tie my %foo, 'Hash::Default', 42;
print $foo{bar};
 
S

Steve Grazzini

Steve Grazzini said:
You could do it with TIEHASH [ ... ]

sub TIEHANDLE {

Silly of me; I guess that means you can just use the default
version of TIEHASH.
 
G

Glenn Jackman

Experienced but Undocumented said:
Newbie question: I've got an array like so:
$foo{'bar'} = "test";

Is there a way to assign a default value to any undefined values? For
example, if the program requests $foo{'abc'} and we haven't defined it, for
the default value to be used?

$foo{$key} = $default unless exists $foo{$key};
or
$foo{$key} ||= $default;

The second is a different test: it assigns the default value if the
hash value is undefined. A hash key can exist but have an undefined
value:
$foo{bar} = "test";
$foo{abc} = undef;
@keys = keys %foo; # @keys is ('bar', 'abc') in some order
 
E

Experienced but Undocumented

Experienced but Undocumented said:
Newbie question: I've got an array like so:
$foo{'bar'} = "test";

Thanks for your prompt replies, everyone!
 
T

Tad McClellan

Glenn Jackman said:
$foo{$key} = $default unless exists $foo{$key};
or
$foo{$key} ||= $default;


But neither of those do what the OP asked for.

He didn't say he wants to add entries to the hash.

He wants something other than undef to be returned when
exists() is false.

tie()ing is the way to go.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top