Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Argument isn't numeric in sort -- resolve or ignore?

by Argel (Prior)
on Feb 25, 2016 at 22:07 UTC ( [id://1156146]=perlquestion: print w/replies, xml ) Need Help??

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

I get the warning when $a or $b is 'etc' or "ccp". and it seems to be tied to the two lines I commented on. The if statement above them does not cause any problems, but I assume that's because neither site has any numbers in them(?). Is there something I can do to eliminate the warnings or can I just ignore them (and add a no warnings numeric)? Using Perl 5.16.3.
my %site = ( 24j1 => { count=>1, devices=>['192.168.0.1'] }, 1924 => { count=>3, devices=>['192.168.1.1','192.168.1.3','192 +.168.1.9'] }, etc => { count=>2, devices=>['192.168.2.1','192.168.2.23'] }, ccp => { count=>1, devices=>['192.168.3.1'] }, ); sub my_sort { if( ("$a" ~~ @datacenters) && ("$b" ~~ @datacenters) ) { return $site{$b}->{count} <=> $site{$a}->{count}; } if( ("$a" ~~ @datacenters) && ! ( "$b" ~~ @datacenters) ) { retu +rn $a } # Warnings go away if I if( ("$b" ~~ @datacenters) && ! ( "$a" ~~ @datacenters) ) { retu +rn $b } # comment out these two lines. if( ( $a =~ $eu_regex ) && ( $b !~ $eu_regex ) ) { retu +rn $a } if( ( $b =~ $eu_regex ) && ( $a !~ $eu_regex ) ) { retu +rn $b } return $site{$b}->{count} <=> $site{$a}->{count}; } foreach my $site ( sort my_sort keys %site ) {do_stuff_with($site) }

Elda Taluta; Sarks Sark; Ark Arks
My deviantART gallery

Replies are listed 'Best First'.
Re: Argument isn't numeric in sort -- resolve or ignore?
by GrandFather (Saint) on Feb 26, 2016 at 02:03 UTC

    You can't ignore the warnings - they highlight an error in your code as suggested by davido.

    Sort expects a compare sub to return -1, 0 or 1 meaning $a sorts before $b, $a and $b sort equivalently, or $b sorts before $a. Other values do not make sense. Returning $a does not make sense (for any usual sort anyway).

    Describe in words what your sort criteria are and either the implementation will come clear to you, or we can help you achieve that.

    Smart match is probably not useful here. Quite likely you can use grep instead.

    Premature optimization is the root of all job security
      Duh!!! I was so focused on the smartmatch operator that I completely overlooked the problems with the return values. Good idea about grep as well. Thanks everyone, you just saved me several more hours of pain!

      Elda Taluta; Sarks Sark; Ark Arks
      My deviantART gallery

Re: Argument isn't numeric in sort -- resolve or ignore?
by davido (Cardinal) on Feb 26, 2016 at 00:17 UTC

    Forget about those two lines for a moment. Why would your comparator return $a, or $b, or -1, 0, 1? The first and final returns will return the result of N <=> M, whereas all the other returns return the value being compared instead. That's got to be a bug.


    Dave

      A bug where, sir? In perl, or in the OPs code? I'm legitimately interested in any details/feedback you can provide.

        In the OP's code. The return $a lines will return values like 'etc'. Sort expects -1, 0 or 1 as is returned by cmp and <=>.

        Premature optimization is the root of all job security

        GrandFather did a better job of explaining it than I did.

        sort expects the comparator subroutine to return a value less than zero, equal to zero, or greater than zero for the conditions of left < right, left == right, and left > right. So the use of cmp and <=> is commonplace. But some of the possible return values of the OP's comparator are strings, which will probably not be of much use to sort. It seems as though the OP thinks he should return the value that has higher precedence in the comparison, which is a fallacy, and except for unusual circumstances, indicative of a bug in the code.


        Dave

Re: Argument isn't numeric in sort -- resolve or ignore?
by Anonymous Monk on Feb 25, 2016 at 23:54 UTC
    it seems to be tied to the two lines I commented on
    "It seems"? So the problem is that you don't understand what your program actually does. That's okay. There is smartmatch there... I don't understand smartmatch either. Let's face the terrible truth - smartmatch is just too smart for us.

    So don't use smartmatch.

      The smartmatch operator, ~~, is to the most recent version of bleadperl (5.23.8) considered experimental (and has been for a very long time).

      I haven't read up on any tickets recently on it, but it most certainly shouldn't be relied upon.

      This smartmatch operator was introduced in 5.10.0, and if one reads through docs, wasn't ever really 'liked'. The introduction was in perldelta 5.10.0.

      As with any experimental feature, one should refrain from using it in any production code, and refer to the most recent perldelta to see what will live and/or die.

      Data::Compare may help here.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-19 13:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found