Re: Help: Process many files at the same time
- From: Tad J McClellan <tadmc@xxxxxxxxxxxxxx>
- Date: Tue, 30 Sep 2008 08:57:35 -0500
Amy Lee <openlinuxsource@xxxxxxxxx> wrote:
foreach $file (@ARGV)
Why do you think you need this foreach loop?
(hint: where does your program make use of the $file variable?)
{
while (<>)
The diamond operator (<>) opens a file (for reading) for you.
{
chomp;
if (/(\d+)\s+dG = (-?[\d.]+)\s+(.*)/)
{
$total = $1;
$name = $3;
s/.*//;
What is in $_ at this point in your program?
(hint: print it out here.)
}
$g += tr/C//;
$c += tr/G//;
What string is the tr/// operator operating on here? (Answer: the string in $_).
What is in $_ at this point? (Answer: either the empty string or a newline).
How many "C" characters are there in $_ (Answer: zero).
Repeatedly adding zero to a number will not likely lead to useful results...
}
$gc = $g+$c;
$GC = $gc/$total;
I anticipate that you will run into yet another Frequently Asked Question,
so let's get that out of the way right now:
perldoc -q 999
Why am I getting long decimals (eg, 19.9499999999999) instead
of the numbers I should be getting (eg, 19.95)?
and
perldoc -q round
Does Perl have a round() function? What about ceil() and floor()?
Trig functions?
print "$name\t$GC\n";
Since you have not provided a filehandle to print(), all of the output
will go to the default stream (STDOUT).
If you want to write to a file, you must open() it for writing,
and use the filehandle that the open() set up for you, eg:
open my $OUTFILE, '>', "$file.new" or die "could not open '$file.new' $!";
then
print $OUTFILE "$name\t$GC\n";
}
Then I hope I can get a list of the result because I use "foreach" to
process many files I gave at the same time. However, It just output one
file and the output variable $CG is wrong. If I run this to process one
file, it could work well.
perl will handle all of the reading from one file then writing to
another file for you.
See the -i switch in perlrun.pod and the $^I variable in perlvar.pod.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
.
- References:
- Help: Process many files at the same time
- From: Amy Lee
- Help: Process many files at the same time
- Prev by Date: how to replace $1, $2, $3 ...
- Next by Date: Re: use Class::Std :ATTR
- Previous by thread: Re: Help: Process many files at the same time
- Next by thread: Question about regex (nagios plugin)
- Index(es):
Relevant Pages
|