in reply to handling tab delimited files

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.

Replies are listed 'Best First'.
Re^2: handling tab delimited files
by shaludr (Initiate) on May 05, 2010 at 17:00 UTC
    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);

        The code has started to work finally.. but the problem is it is giving the output in one column and splitting all the values.

        so for the line :

        0.250 0.413 0.432 0.345 0.786 1.001 0.987

        the output is :

        125 206.5 216 172.5 393

        and so on

        how should i put it back together? This is the code i used :

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