Undefined array values

Discussion in 'Perl Misc' started by Experienced but Undocumented, Oct 23, 2003.

  1. 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
     
    Experienced but Undocumented, Oct 23, 2003
    #1
    1. Advertising

  2. 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
     
    Andreas Kahari, Oct 23, 2003
    #2
    1. Advertising

  3. Experienced but Undocumented <> 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::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};

    --
    Steve
     
    Steve Grazzini, Oct 23, 2003
    #3
  4. Steve Grazzini <> 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
     
    Steve Grazzini, Oct 23, 2003
    #4
  5. Experienced but Undocumented <> 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
     
    Glenn Jackman, Oct 23, 2003
    #5
  6. > 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" <> wrote
    > That's a hash, but never mind.


    What I meant *blushes* :p
     
    Experienced but Undocumented, Oct 23, 2003
    #6
  7. "Experienced but Undocumented" <> 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!
     
    Experienced but Undocumented, Oct 23, 2003
    #7
  8. Glenn Jackman <> wrote:
    > Experienced but Undocumented <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 23, 2003
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mantorok Redgormor
    Replies:
    70
    Views:
    1,764
    Dan Pop
    Feb 17, 2004
  2. Boris Nieuwenhuis

    controls en undefined values

    Boris Nieuwenhuis, Nov 6, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    105
    Boris Nieuwenhuis
    Nov 6, 2003
  3. Dmitry Epstein

    Assigning split to a list: undefined values?

    Dmitry Epstein, Nov 22, 2003, in forum: Perl Misc
    Replies:
    10
    Views:
    184
    Malcolm Dew-Jones
    Nov 26, 2003
  4. VK
    Replies:
    45
    Views:
    606
    Dr John Stockton
    Sep 12, 2006
  5. -Lost
    Replies:
    13
    Views:
    373
    Richard Cornford
    Jan 31, 2007
Loading...

Share This Page