Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Uninit Value - Hash Value Compare

by kchinger (Initiate)
on Nov 05, 2020 at 15:14 UTC ( [id://11123420]=perlquestion: print w/replies, xml ) Need Help??

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

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.

Replies are listed 'Best First'.
Re: Uninit Value - Hash Value Compare
by choroba (Cardinal) on Nov 05, 2020 at 15:59 UTC
    That's not the code you're running. It doesn't even compile! There is a missing semicolon on the grep line.

    Also, note that

    my $hash = %$hashRef;

    is most probably wrong, did you mean

    my %hash = %$hashRef;
    instead? Your version populates a scalar variable $hash with the size of the hash and the rest of the code operates on the %hash variable from the outer scope.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      yeah, as I mentioned, I have to re-type the code, and I had to remove code/anonymize variable names and stuff because it's in a secure area that I can't copy/paste from and can't get on the internet on.

      And yes, I actually have my %hash = %$hashRef; Just a typo in what I put here, sorry.

      I think the missing semicolon it's getting away with because it's the last line in the block, that was that way in my working code. I fixed it for accuracy in my code, no changes, obviously.

      Sorry. I can try a better way to get it out of the system and anonymize it better so it actually runs fully if I need to.

Re: Uninit Value - Hash Value Compare
by kchinger (Initiate) on Nov 05, 2020 at 19:54 UTC

    I apologize for wasting everyone's time. It was right in front of me, and I ended up fixing the issue by attempting to anonymize, which is why you couldn't find it.

    Before I changed names, I had something more like below:

    foreach my $key (sort { $hashNew{$a} cmp $hashOld{$b} } keys %hashOld)

    In making the short one I changed the hash names, and made them the same, because I knew that's what I was supposed to do, but didn't notice that I had the wrong variables. My hashes were similar enough that I only got a few errors, instead of thousands, because the key existed in both in most cases

Log In?
Username:
Password:

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

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

    No recent polls found