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

Dear all,

I've done some reading and it sounds like it is possible to have numeric hash keys and perl is supposed to figure out when numbers should act as numbers and when they should act as strings. However, I am getting an error message that indicates to me that it thinks my numbers are strings. Any advice appreciated. Thanks

use strict; use warnings; use Data::Dumper; ############################ my $inGNPS="MsRtLib_all.tab"; open(IN,$inGNPS); my %HoPMAnnot; my %HoPMrtAnnot; while(my $line=<IN>){ chomp $line; my ($ParMass,$RT,$annot)=split(/\t/,$line); $HoPMAnnot{$ParMass}=$annot; $HoPMrtAnnot{$ParMass}{$RT}=$annot; } close(IN); print Dumper (%HoPMAnnot); ###### my $inATdat="consensus_features_extracted.csv"; open(IN,$inATdat); while(my $line=<IN>){ chomp $line; next if($line =~/^,/); # header starts with comma my ($feat_numb,$mz_min,$mz_max,$rt_min,$rt_max,$quality,$inten +sity)=split(/,/,$line); foreach my $PM (keys %HoPMAnnot){ if ($PM <=$mz_max && $PM >=$mz_min){ print join("\t",$line,$HoPMAnnot{$PM})."\n"; } } } close(IN);

data dumper output

$VAR1 = '272.043'; $VAR2 = 'N/A'; $VAR3 = '272.188'; $VAR4 = 'N/A'; $VAR5 = '838.844'; $VAR6 = 'Sodium_Formate'; $VAR7 = '601.393'; $VAR8 = 'N/A';

error

Argument "parent mass" isn't numeric in int at checkTheoFeat4GNPSannot.pl line 25, <IN> line 1034.

Replies are listed 'Best First'.
Re: numeric hash keys
by Corion (Patriarch) on Mar 06, 2015 at 13:44 UTC

    Have you looked at the code in line 25? Have you looked at the value of $PM?

    The int function expects a number and warns you if it gets something that does not look like a number.

    For your further understanding, hash keys can only be "strings", but Perl converts automatically between strings and numbers depending on the operators you use on the scalars containing the values.

Re: numeric hash keys
by RichardK (Parson) on Mar 06, 2015 at 14:03 UTC

    You'll get better looking output from Dumper if you pass it a reference to your hash, and that should help you see what's going on. i.e

    print Dumper(\%HoPMAnnot);
Re: numeric hash keys
by Anonymous Monk on Mar 06, 2015 at 13:48 UTC

    Read the error message again: You are passing the string "parent mass" to int, that's the source of the error message.

    Hash keys are always stringified, but like any string Perl will automatically convert it back to a number if used in a numeric context (if it can).

      sorry I added the int($PM) to try and fix the problem but it didn't work. I get the same error without the int($PM) line. if I use <= in the if statement then why doesn't it treat the keys like a number?

        Please re-read our replies.

        When you use a value in numeric context, for example by using the <= operator on it, Perl expects a number. But you are handing it something else. Perl even tells you what it saw instead, but you seem to hide that value from us.

        Find out what the offending value is and then look at your code and think about how that value gets there.

        Most likely, printing $PM and thinking long and hard about the difference between what you expect in $PM and what you get might give you a hint.

        I get the same error without the int($PM) line.

        No, you don't - the error message will be different. Please provide the exact error messages and the exact code used to produce them.

        You've also edited your original post without marking it updated. Don't do that, see here for why.

        why doesn't it treat the keys like a number?

        If $PM is "parent mass", how do you expect Perl to use that as a number?

        I suspect your problem is in the input data.

        Please read and follow How do I post a question effectively? - please show the exact code used, the exact error message, and the exact sample input used to generate that error.