in reply to Re: handling tab delimited files
in thread handling tab delimited files

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

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

    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);
        how should i put it back together?

        You could write

        foreach my $val (@values){ $val *= $df * 50 ; } print join("\t", @values), "\n";

        This would modify the values in-place in the array (because $val is an alias for the respective elements in the array), so you can simply join them with a tab after the for loop.

        Or shorter, with map:

        print join("\t", map { $_ * $df * 50 } @values), "\n";

        or maybe even

        ... while (<FILE>){ print join("\t", map { $_ * $df * 50 } split /\t/), "\n"; }
        yeeeeeeeeeeeee my code is working... I know i m just posting on my own... but this is what is did!!
        print " Enter the number. \n"; chomp ($df = <STDIN>); open FILE, "45well.txt" or die $!; while (<FILE>){ chomp $_; @values = split('\t', $_); foreach $val (@values){ $DNA_conc = $val * $df * 50 ; print $DNA_conc; print"\t"; } } print "\n"; close (FILE);
        any ideas on how to Print the converted values out into another tab-delimited file of equal dimensions (12 x 8), with a header in the file to tell what was done. thank you so much toolic and almut for your help