Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^3: while loop question

by aitap (Curate)
on Aug 21, 2014 at 11:48 UTC ( [id://1098223]=note: print w/replies, xml ) Need Help??


in reply to Re^2: while loop question
in thread while loop question

exists should help you with this problem. As you may have noticed, looping over keys of a hash is much slower than accessing it by key. By the way, very often it is plain wrong.

Replies are listed 'Best First'.
Re^4: while loop question
by adriang (Sexton) on Aug 21, 2014 at 11:54 UTC

    "exisys" is not good for me at leat for this crrent problem because I use AnyDBM_File:

    AnyDBM_File doesn't define an EXISTS method at /gd/Development/Perl/Pr +actice/www/cgi-bin/submitcoffee.pl line 32.

      "EXISTS", "exisys" and "exists" are all different words.

      exists is not a method. It is a basic built-in perl function. It is completely and utterly unrelated to AnyDBM_File. It tells you if a key exists in a hash; that is what you want, no need for looping.

      Read the documentation linked by aitap for details.

      I can reproduce this error message: when I tie a hash using AnyDBM_File, it selects NDBM_File, which implements Tie::Hash without a sub EXISTS { ... }, so exists on the tied hash does not work.

      However, defined replaces exists in this case, because NDBM_File cannot store an undef in the database:

      #!/usr/bin/perl use Data::Dumper; use Fcntl; use AnyDBM_File; die $! unless tie %h, 'AnyDBM_File', 'test.db', O_RDWR()|O_CREAT(), 06 +00; undef $h{'b'}; # try to store an undef print Dumper(\%h); # empty string is stored instead print "still defined\n" if defined $h{'b'}; # we can confirm its existence: # if it's defined, it does exist # and no undefs exist there delete $h{'b'}; # delete works as expected print "does not exist\n" unless defined $h{'b'}; print Dumper(\%h); __END__ $VAR1 = { 'b' => '' }; still defined does not exist $VAR1 = {};
      So using defined turns out to be the right decision.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1098223]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (1)
As of 2024-04-25 04:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found