Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Hashing: Argument "" isn't numeric in sort

by P0w3rK!d (Pilgrim)
on May 20, 2003 at 14:29 UTC ( [id://259465]=perlquestion: print w/replies, xml ) Need Help??

P0w3rK!d has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I am dumping a hash table as follows:

sub myMethod { ... foreach $keyOut (sort {$a <=> $b} keys %hshOut) { ... } ... }
The error is as follows:

Argument "" isn't numeric in sort at C:/Perl/site/lib/Foo/A.pm line 163.

The keys are in the format #.#. How do I get rid of this error without changing my keys?

Thank you :)

-P0w3rK!d

Replies are listed 'Best First'.
Re: Hashing: Argument "" isn't numeric in sort
by hardburn (Abbot) on May 20, 2003 at 14:35 UTC

    The <=> operator only works in numeric context. You need the cmp operator instead, which works in string context:

    foreach $keyOut (sort {$a cmp $b} keys %hshOut) { ... }

    Alternative: use Data::Dumper to dump your hash (assuming you're doing this for debugging purposes and not dumping it for user-viewable data).

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      The keys look something like 465.274. The keys used to be numeric and then they became strings. Thanks for catching this for me.
      -P0w3rK!d
        It's quite impossible to change a hash-key, they are constant strings - strictly speaking they cannot even be numbers.

        The string "465.274" is converted to a number when needed by perl, so that should not give you this warning, as the following code shows:

        #!/usr/bin/perl -wl use strict; my @array = qw(465.274 21893.32 72.37); # qw creates quoted strings print for (sort {$a <=> $b} @array);

        output:

        72.37 465.274 21893.32

        No warnings at all.

        Joost

        -- #!/usr/bin/perl -np BEGIN{@ARGV=$0}s(^([^=].*)|=)()s; =Just another perl hacker\
Re: Hashing: Argument "" isn't numeric in sort
by Joost (Canon) on May 20, 2003 at 14:37 UTC
    "" isn't a number, so probably your hash keys are not in numeric format (if you mean # to be a digit). Your problems lie elsewhere.

    Joost

    -- #!/usr/bin/perl -np BEGIN{@ARGV=$0}s(^([^=].*)|=)()s; =Just another perl hacker\
Re: Hashing: Argument "" isn't numeric in sort
by edoc (Chaplain) on May 20, 2003 at 15:43 UTC

    Sounds like you've got an empty key in there. One fix might be to delete that slice, but you'd be better off figuring out how it got in there in the first place..

    delete $hshOut{''}; foreach $keyOut (sort {$a <=> $b} keys %hshOut) { ... }

    cheers,

    J

Log In?
Username:
Password:

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

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

    No recent polls found