user3781528 user3781528 - 5 months ago 9
Perl Question

Converting string to number “Argument isn’t number error”

I’m parsing tab delimited files. Several columns are not being recognizes as numbers even though they are clearly numeric. When I try to sum up these values errors are display : Argument ""97"" isn't numeric in addition (+) and Perl returns 0.

I’ve tried using Scalar::Util qw(looks_like_number); but it produced the same result '0'. Is there something else I can try? Thanks

Here is the code:

open my $out_fh, '>', $final_variants or die qq{Unable to open "$final_variants" for output: $!};

open my $in_fh, '<', $tsv_file_new
or die qq{Unable to open "$tsv_file_new" for input: $!};

while ( <$in_fh> ) {

my @fields = split;

my $forward_reference = $fields[67];
my $reverse_reference = $fields[68];
my $forward_variant_reads = $fields[77];
my $reverse_variant_reads = $fields[78];

my $total_reads = (looks_like_number($forward_reference)) + (looks_like_number($reverse_reference)) + (looks_like_number($forward_variant_reads)) + (looks_like_number($reverse_variant_reads));

my $current_final_line = $headerline . "\t"
. $forward_reference . "\t"
. $reverse_reference . "\t"
. $forward_variant_reads . "\t"
. $reverse_variant_reads . "\t"
. $total_reads . "\t"
print $out_fh $current_final_line, "\n";
}
}

Answer

Your error message already says it: Argument ""97"" isn't numeric. This happens when the number is actually a string which is surrounded by quotes ("), like so:

my $num = '"42"';
my $sum = $num + 1;

This gives:

Argument ""42"" isn't numeric in addition (+) at...

Try to get rid of the quotes from your numbers:

$num =~ s/"//g;