Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Undefined array values

Reply
Thread Tools

Undefined array values

 
 
Experienced but Undocumented
Guest
Posts: n/a
 
      10-23-2003
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


 
Reply With Quote
 
 
 
 
Andreas Kahari
Guest
Posts: n/a
 
      10-23-2003
In article <wLVlb.160288$pl3.35654@pd7tw3no>, Experienced but Undocumented wrote:
> 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").


--
Andreas Kähäri
 
Reply With Quote
 
 
 
 
Steve Grazzini
Guest
Posts: n/a
 
      10-23-2003
Experienced but Undocumented <(E-Mail Removed)> wrote:
> 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:efault;
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:efault', 42;
print $foo{bar};

--
Steve
 
Reply With Quote
 
Steve Grazzini
Guest
Posts: n/a
 
      10-23-2003
Steve Grazzini <(E-Mail Removed)> wrote:
> You could do it with TIEHASH [ ... ]
>
> sub TIEHANDLE {


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

--
Steve
 
Reply With Quote
 
Glenn Jackman
Guest
Posts: n/a
 
      10-23-2003
Experienced but Undocumented <(E-Mail Removed)> wrote:
> 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

--
Glenn Jackman
NCF Sysadmin
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Experienced but Undocumented
Guest
Posts: n/a
 
      10-23-2003
> In article <wLVlb.160288$pl3.35654@pd7tw3no>, Experienced but Undocumented
wrote:
> > Newbie question: I've got an array like so:
> > $foo{'bar'} = "test";



"Andreas Kahari" <(E-Mail Removed)> wrote
> That's a hash, but never mind.


What I meant *blushes*


 
Reply With Quote
 
Experienced but Undocumented
Guest
Posts: n/a
 
      10-23-2003
"Experienced but Undocumented" <(E-Mail Removed)> wrote in message
news:wLVlb.160288$pl3.35654@pd7tw3no...
> Newbie question: I've got an array like so:
> $foo{'bar'} = "test";


Thanks for your prompt replies, everyone!


 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      10-23-2003
Glenn Jackman <(E-Mail Removed)> wrote:
> Experienced but Undocumented <(E-Mail Removed)> wrote:


>> 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;



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.


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
typeof x == 'undefined' or x == undefined? -Lost Javascript 13 01-31-2007 12:04 AM
undefined vs. undefined (was: new Array() vs []) VK Javascript 45 09-12-2006 05:26 PM
undefined behavior or not undefined behavior? That is the question Mantorok Redgormor C Programming 70 02-17-2004 02:46 PM
Assigning split to a list: undefined values? Dmitry Epstein Perl Misc 10 11-26-2003 10:58 PM
controls en undefined values Boris Nieuwenhuis ASP .Net Web Controls 0 11-06-2003 04:04 PM



Advertisments