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

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

Replies are listed 'Best First'.
Re^6: handling tab delimited files
by graff (Chancellor) on May 06, 2010 at 06:49 UTC
    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