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

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

Replies are listed 'Best First'.
Re^5: handling tab delimited files
by almut (Canon) on May 05, 2010 at 17:47 UTC
    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 ?
        I want to insert a header to the output file

        Just print it :)  — before printing the rows with the numbers:

        print "my header ...\n"; while (<FILE>){ ...
        I want the results to be printed in a new file.

        Either redirect the program's output (stdout) to a file using the shell, or explicitly open another file for output, and then print to that file handle:

        ... open OUTFILE, ">", "outfile.txt" or die $!; print OUTFILE "my header ...\n"; while (<FILE>){ print OUTFILE join("\t", map { $_ * $df * 50 } split /\t/), "\n"; }
        it has to be 7 X 10. how should i set values to 7 columns X 10 rows ?

        There isn't really anything to "set".  If you print 10 lines (rows) with each containing 7 tab separated values, you're done, essentially... i.e. the file will have 7 columns X 10 rows.  This would happen without further ado (with the code we have so far), provided the input file also has the same dimensions (which is seems to have (at least the columns), judging by your sample data).

Re^5: handling tab delimited files
by shaludr (Initiate) on May 05, 2010 at 17:53 UTC
    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