Hi Monks,
I have this application at work where I need to produce files, but where there is no desire to do so in the event the files would be empty.
At a stage in the code, I have a hash. I check to see if the hash has at least one value. If it does, at the next portion of code, its contents are printed out to a file.
The problem is, the program produced files of zero file size. In my investigation, I found that for such an occurrence, the hash actually had one key,value pair. However,
when the code proceeded to print out the contents of the hash, there was nothing printed out!
Can someone share with me why this is happening and how I can correctly identify the hash contains at least one key,value pair in such a way that I can next print out all of the contents of the hash?
Here is the code before some added test snippets.
my $count = 0;
while (my($key, $value) = each(%ip_discover_count) and $count == 0)
{
$count++;
}
if ($count > 0)
{
my $seedFileName = "$CC\_discover.txt";
open (SEED,">$dir/$seedFileName");
while ((my $key, my $value) = each(%ip_discover_count))
{
$ip = &trim($key);
my $snmp_read = &trim($ip_discover_snmp{$key});
print SEED "$ip\t\$snmp_read\n";
}
close (SEED);
}
And here is the code with some checks, where I verified the hash had a key, value pair and whose contents did not print out in the second while loop.
my $count = 0;
my $seedFileName = "$CC\_discover.txt";
open (SEED,">$dir/$seedFileName");
while (my($key, $value) = each(%ip_discover_count) and $count == 0)
{
print SEED "Key: $key\tValue: $value\n";
$count++;
}
if ($count > 0)
{
my $seedFileName = "$CC\_discover.txt";
#open (SEED,">$dir/$seedFileName");
print SEED "Count:\t$count\n";
while ((my $key1, my $value1) = each(%ip_discover_count))
{
$ip = &trim($key1);
my $snmp_read = &trim($ip_discover_snmp{$key1});
print SEED "xx$ip\xx\n";
}
close (SEED);
}
Thanks much for any help,
Tony
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.