Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Use of uninitialized value in string comparison (cmp)

by Random_Walk (Prior)
on Sep 16, 2015 at 10:39 UTC ( #1142184=perlquestion: print w/replies, xml ) Need Help??

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

Good $localtime fellow monks

I have a large piece of code, and while debugging, the output routine is giving me the above message, lots. I am passing it an array of hasrefs (alerts basicaly), and trying to sort by the msg value in the hashrefs. I have distilled the problem to a one-liner. TBH, I expected the problem to drop out elsewhere in my code, surprised I see it in this:

perl -wle '@r=({k=>"r"},{k=>"s"},{k=>"t"});print $_->{k} for sort { $_ +->{k} cmp $_->{k} } @r' Use of uninitialized value in string comparison (cmp) at -e line 1. Use of uninitialized value in string comparison (cmp) at -e line 1. Use of uninitialized value in string comparison (cmp) at -e line 1. Use of uninitialized value in string comparison (cmp) at -e line 1. Use of uninitialized value in string comparison (cmp) at -e line 1. Use of uninitialized value in string comparison (cmp) at -e line 1. r s t

I get the same result if I deref it this way to:

perl -wle '@r=({k=>"r"},{k=>"s"},{k=>"t"});print $_->{k} for sort { $$_{k} cmp $$_{k} } @r'

Why does this happen, where is it finding unitialised values? and of course, how can I fix it?

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

Replies are listed 'Best First'.
Re: Use of uninitialized value in string comparison (cmp)
by Discipulus (Abbot) on Sep 16, 2015 at 10:49 UTC
    Hello,

    sort needs to operate on $a and $b not on $_
    #sorry win quote perl -wle "@r=({k=>'r'},{k=>'s'},{k=>'t'});print $_->{k} for sort { $a +->{k} cmp $b->{k} } @r;" r s t

    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Aaarrgghhh, I knew I had been staring at it too long. I knew that, but it slipped out my memcache somewhere :(. Thanks for the reminder.

      Cheers,
      R.

      Pereant, qui ante nos nostra dixerunt!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2022-01-24 15:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (64 votes). Check out past polls.

    Notices?