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

I have a tab delimited file and I want to multiply each value in the file with 50 and the user input value ($df). My code is giving me following error :

Argument "6 20 40 " isn't numeric in multiplication (*) at - line 9, <FILE> line 3.

use warnings; print " Enter the dilution factor. \n"; chomp ($df = <STDIN>); open FILE, "45well.txt" or die $!; while (<FILE>){ chomp $_; @values = split('\t', $_); $val = @values; foreach $val (@values){ $DNA_conc = $val * $df * 50 ; print $DNA_conc; print"\n"; } } close (FILE);

Replies are listed 'Best First'.
Re: handling tab delimited files
by almut (Canon) on May 05, 2010 at 16:52 UTC

    Maybe the file isn't tab delimited, as you think it is. In this case, the split wouldn't work, and $val (in the multiplication) would be entire line...

    To debug such issues yourself, just print out the value(s) in question, in this case $val.

    Also, the line $val = @values; doesn't make any sense here — it's superfluous.

      open FILE, "96well2.txt" or die $!; while (<FILE>){ chomp $_; @values = split('\t', $_); $val = @values; print $val; } close (FILE);
      This is what printing $val gives 111111111111

        As $val = @values assigns the number of elements in the array to $val, your output confirms my suspicion, i.e. that the lines aren't tab separated, so you get only one value in @values (the entire line) from the split.

        Try splitting on whitespace:

        #!/usr/bin/perl use strict; use warnings; print " Enter the dilution factor. \n"; chomp (my $df = <STDIN>); open FILE, "45well.txt" or die $!; while (<FILE>){ chomp; my @values = split ' '; foreach my $val (@values){ my $DNA_conc = $val * $df * 50 ; print "$DNA_conc\n"; } } close (FILE);
Re: handling tab delimited files
by toolic (Bishop) on May 05, 2010 at 16:52 UTC
    print is a fabulous debugging tool. Show us what $val and $df are:
    use warnings; print " Enter the dilution factor. \n"; chomp ($df = <STDIN>); open FILE, "45well.txt" or die $!; while (<FILE>){ chomp $_; @values = split('\t', $_); $val = @values; foreach $val (@values){ print "df >>>$df<<<\n"; print "val >>>$val<<<\n"; $DNA_conc = $val * $df * 50 ; print $DNA_conc; print"\n"; } } close (FILE);

    That's Tip #2 from the Basic debugging checklist.

      $df = is the user input value. so if i enter 10 $df = 10. $ val is giving the output as 111111. so something is wrong here. the file looks like this
      0.250 0.413 0.432 0.345 0.786 1.001 0.987 0.984 0.125 0.678 0.243 0.657 0.342 0.743 0.986 0.734 0.764 0.650 0.457 0.965 0.836
        When I copied your data, I get single spaces instead of tab characters. Try to use split as follows:
        use strict; use warnings; my $df = 10; while (<DATA>) { chomp $_; my @values = split; foreach my $val (@values) { my $DNA_conc = $val * $df * 50; print $DNA_conc; print "\n"; } } __DATA__ 0.250 0.413 0.432 0.345 0.786 1.001 0.987 0.984 0.125 0.678 0.243 0.657 0.342 0.743 0.986 0.734 0.764 0.650 0.457 0.965 0.836

        This is the output I get:

        125 206.5 216 172.5 393 500.5 493.5 492 62.5 339 121.5 328.5 171 371.5 493 367 382 325 228.5 482.5 418