Hey, have what is probably a simple question, but I'm struggling a bit and I've checked what I know to check. I have a hash to sort by value, and I'm getting "Use of uninitialized value in string comparison (cmp) at blahblah line 66." when I sort it. But I've checked and don't seem to have any empty values. Hopefully I don't make a syntax error, I have to type it because it's in a secure system so I can't copy/paste and I had to anonymize function names and hash names and stuff. It does run, and it sorts the hashes fine.
Code:
#!/usr/bin/perl
use Win32;
use strict;
use warnings;
use v5.10.10;
use Data::Dumper;
user constant {
DEBUG => 1,
}
my %hash;
my $counter;
#hash has stuff put in it here
$counter = &compare_hash(\%hash);
sub compare_hash{
my $hashRef = shift;
my $hash = %$hashRef;
my $counter = 0;
print Dumper(\%hash); #searching for undef or blank ('') values in
+ the dump output comes up empty
print "$_ has undef as value\n" for grep {not defined $hash{$_}} k
+eys %hash #this comes up empty, thanks to Corion for the
#suggestion
foreach my $key (sort { $hash{$a} cmp $hash{$b} } keys %hash){ #th
+is is line 66
#do something
#print and check and stuff on $hash{$key}
++$counter;
}
return $counter;
}
Thanks in advance.
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.