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

Dear experts: The line
   $value = $hashref->{$fieldnum};
is triggering the 2 errors
   Argument "$/  1l" isn't numeric in hash element
   Bad index while coercing array into hash
The program then halts.

The value "$/ 1l" is the correct $value for the $fieldnum key. In other words, both the key and the element are correct.

I don't understand the problem here. Elements need not be numeric. And why is Perl trying to use the element as an argument? And to what?

I thought it might be the $ which is the first data character, but the same line of code handles similar data OK before it gets to this one.

Many thanks for any insights!

Replies are listed 'Best First'.
Re: Argument isn't numeric in hash element
by haoess (Curate) on Jun 09, 2004 at 15:17 UTC

    Do you use pseudo hashes? I could reproduce your error message using v5.8.4:

    #!/usr/bin/perl -l use warnings; no warnings 'deprecated'; use strict; my $fieldnum = 1; my $hashref = [ { $fieldnum => '$/ 1l' } ]; my $value = $hashref->{ $fieldnum }; print $value;

    If you're using pseudo hashes: That's not the way it works. The first element of your pseudo hash array is an anonymous hash with keys as names to get access to the array indices mentioned as hash values. You get access to arrays using indices, so the values must be numeric.

    Pseudo hashes are deprecated as of v5.8.0, quoting perl58delta:

    The fields pragma interface will remain available. The restricted hashes interface is expected to be the replacement interface (see Hash::Util). If your existing programs depends on the underlying implementation, consider using Class::PseudoHash from CPAN.

    I don't know if that's the thing you're doing, so I can't provide any solution so far.

    -- Frank

Re: Argument isn't numeric in hash element
by artist (Parson) on Jun 09, 2004 at 14:24 UTC
    Your errors suggests that provide us the code before this line which may has anything to do with this variables.
    Both of the code below works.:
    $hashref = { '$/ 11' => 'a'}; $fieldnum= '$/ 11'; $value = $hashref->{$fieldnum}; print "$value";
    and
    $hashref = { a => '$/ 11' }; $fieldnum= 'a'; $value = $hashref->{$fieldnum}; print "$value";
Re: Argument isn't numeric in hash element
by hv (Prior) on Jun 09, 2004 at 16:32 UTC

    As haoess suggests, pseudohashes are causing the problem. However you may not have intended to create one: if you have inadvertently assigned an arrayref of a particular form to $hashref then I'd expect error messages along the lines you are seeing.

    The solution: look back earlier in the code to where $hashref is set, and perhaps add some diagnostics around that point to find out why it isn't getting set to a hashref as you clearly expected.

    This sort of problem is exactly why pseudohashes have now been deprecated, and will be removed in the next major release of perl.

    Hugo

      Frank and Hugo,

      I am not intentionally using pseudo hashes, but I am using an array of hashes. Perhaps the code for generating these is messed up.

      I appreciate the help. I think it will point me to where my problem is.

      Many thanks!