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

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);

Replies are listed 'Best First'.
Re^4: handling tab delimited files
by shaludr (Initiate) on May 05, 2010 at 17:40 UTC

    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"; }
        Hey thanks a lot. This is working. I want to insert a header to the output file ? do you know how i can do that? I want the results to be printed in a new file. it has to be 7 X 10. how should i set values to 7 columns X 10 rows ?
      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
        any ideas on how to Print the converted values out into another tab-delimited file of equal dimensions

        Just open an output file, and print to that file handle, instead of printing to (the default) STDOUT; that is:

        ... open OUT, ">", "outfile.name" or die "outfile.name: $!\n"; ... print OUT $DNA_conc; # and so on...
        Or, you could just use redirection on the shell command line, but for that to be effective, you want to use @ARGV for user input to the script, rather than printing a prompt to ask for input -- that is:
        # don't do this: # print " Enter the number. \n"; # chomp ($df = <STDIN>); # do this instead: die "Usage: $0 multiplier_value\n" unless ( @ARGV == 1 and $ARG[0] =~ /([\d.]+)/ ); my $df = $1;
        (The sanity check on the @ARGV value is not exactly a perfect one for checking that a string is a valid numeric -- e.g. it won't accept negative numbers -- but it's more than what you've been doing so far, and every little bit helps.)

        Doing it that way, your shell command line would go like this, to set the value (e.g. to "10.5") and redirect output to some other file:

        your_script_name 10.5 > outfile.name
        As a rule, it makes sense to use @ARGV for the input file name too -- in fact, perl defaults to reading file names given as command-line args. I would do your script like this:
        #!/usr/bin/perl use strict; use Regexp::Common; die "Usage: $0 multiplier_number [input.file]\n" unless ( @ARGV and $ARGV[0] =~ /^$RE{num}{real}$/ ); my $df = shift; # take the numeric arg off @ARGV while (<>) # read from STDIN, or from file(s) named in @ARGV { if ( ! /^\s*(?:$RE{num}{real}\s+)+$/ ) { # if line has non-numer +ics print; } else { my @vals = map { $_ * $df * 50 } split; print join( "\t", @vals ), "\n"; } }
        That also uses a better method of checking for numbers. Run it like this:
        script_name 10 input.txt > 10_x_50_x_input.txt