Mathew Snyder wrote: >
Code snippet at the end of my little script:<more code> ... my $count; foreach my $email (@emails){ print $email, "\n"; $count += 1; }; if ($count > 0){ print "\n" . "count = " . $count . "\n"; } else{ print "count = 0"; }; exit; I've declared $count globally so I can use it in more than one block of code. However, when I execute this I get an error saying "Use of uninitialized value in numeric gt (>) at ./check_spam_users.pl line 59." Line 59 being the if ($count > 0) line.If I declared it outside of the scope of any block of code shouldn't it be available in any block of code?
Hi Mathew. As John has pointed out, if there are no emails in the array then $count will never be incremented and will remain undefined unless it is initialised to zero in the declaration. Once you've done that you no longer need the conditional statement and you can simply write print "\ncount = $count\n"; whether the value is positive or not. Oh, and $count += 1 is usually abbreviated as $count++ You also need to be aware that, if you are using the Email::Address module as I proposed, then the elements of @emails are Email::Address objects and not simple strings. Writing just print $email, "\n"; is implicitly "stringifying" the object and returning the phrase, address and comment parts (where they exist) formatted together. This will give you something like Mathew Snyder <[EMAIL PROTECTED]> (perl.beginners) whereas you probably want just the address itself, and should be writing print $email->address, "\n"; which will generate just [EMAIL PROTECTED] And if you do want the full version it is better generated explicitly by writing print $email->format, "\n"; which will produce the same output as you already have but it will be clear what your code is doing. HTH, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>
