sulfericacid has asked for the wisdom of the Perl Monks concerning the following question:

Problem: I am getting use unitialized all over the place in this snippet even though I'm testing if the variable exists before I use it.

Background: If $foo{$bar} exists I want to take the value into a variable, increment the number then reassign it to the database. If $foo{$bar} doesn't exist I assign a value of 0 then increment the same way.

One last question, I was told I had to nest my statements if I wanted to do use an If inside of another If statement an so forth. Problem was I searched the site and the web for what 'nests' are and didn't find anything. Do they mean empty blocks like I'm using?

Thanks so much for your help!

{ if (exists $dbm{$lost}) { $youlose = $dbm{$lost}; $youlose++; $dbm{$lost} = $youlose; } else { $dbm{$lost} = 0; $youlose = $dbm{$lost}; $youlose++; $dbm{$lost} = $youlose; } } }


"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid

Replies are listed 'Best First'.
Re: database unclarity when finding 'unitinitaliazed' everywhere
by pg (Canon) on Mar 17, 2003 at 02:56 UTC
    Not that complex ;-), just do:
    use strict; my %a; $a{"abc"} ++; print $a{"abc"};
    Two things:
    1. In this case you don't need to initilize the hash element, as Perl will initilize it to 0 for you, in a number context.
    2. Just in case you do not know, exists is different from defined. If a hash element has value undef, than it exists, although not defined.

      This demo would help you to understand:
      use strict; my %a; check(); print "now set abc to undef\n"; $a{"abc"} = undef; check(); sub check { print "defined\n" if (defined($a{"abc"})); print "exists\n" if (exists($a{"abc"})); }
      Ugh, I feel like a total moron. I got errors when I originally tried doing $dbm{blah}++ rather than storing them into variables and putting it back together. I'm chosing your way as it's so much quicker to write and you don't have to fuss with making so many extra variables. My way WASN'T wrong, lol, just not as right as your's :P

      I think the error was $lost was undefined afterall, but with your method I didn't need to use variables as a dbm key.

      Thanks for your help!

      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid
Re: database unclarity when finding 'unitinitaliazed' everywhere
by The Mad Hatter (Priest) on Mar 17, 2003 at 03:04 UTC
    Nesting is the inclusion of blocks within other blocks, which usually means indenting everything consistently. So say the second if (inside the first if) is indented 4 spaces, then the statements within the second if are indented 8 spaces total. Make sense? : ) Here is an example:
    if (this) { if (that) { do this } and this }
    That second if is nested inside the first if. The indenting just makes things easier to read.
      I find this difficult to believe because I've tried in numerous scripts to do something like what you said and I got an error. Maybe the environment was different and it was all due to other problems but for the sake of arguement is doing a naked block wrong if that's a preferred choice of the programmer?

      Thanks for your help!

      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid
        No problem, glad to help. Anyway, a naked block is fine to do (and needed in some cases), but in your code above (in the original post), it looks like you have too many brackets.
Re: database unclarity when finding 'unitinitaliazed' everywhere
by robartes (Priest) on Mar 17, 2003 at 07:23 UTC
    Hi,

    your problem is either that $lost is undefined, or, as pg has indicated, that $dbm{$lost} exists, but is undefined. You could check for definedness before going through this snippet:

    if defined ($lost) { if (exists ($dbm{$lost}) && defined ($dbm{$lost}) ) { .. the rest of your snippet } }
    You might also want to check the rest of your code to find out just why either of these two is undefined, and whether that was intentional or not.

    CU
    Robartes-

      Thanks for your help. I'm not sure what the exact problem is because I changed this and that so many times until it finally worked, lol. It must have been because it was undefined.

      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid