Re: Help: Process many files at the same time



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/"
.



Relevant Pages

  • Re: Help: Process many files at the same time
    ... Why do you think you need this foreach loop? ... (hint: where does your program make use of the $file variable?) ... The diamond operator opens a file for you. ... If you want to write to a file, you must openit for writing, ...
    (comp.lang.perl.misc)
  • WWWJDIC in Japanese
    ... The first screen of WWWJDIC using Japanese text messages is ... the WWWJDIC code has the language flag hard-coded. ... get a lot more of the messages translated (hint, ... rework how the binary invokes the messages (at present it opens up ...
    (sci.lang.japan)
  • Re: WWWJDIC in Japanese
    ... with it, and feedback is welcome. ... get a lot more of the messages translated (hint, ... rework how the binary invokes the messages (at present it opens up ... jp "a second word after the keyword will limit the search to entries containing the second word" ...
    (sci.lang.japan)
  • Re: FSU loses !
    ... which isn't a big deal for me since I never roll an o/u into a parlay. ... The average cf line opens at 51 or so, ... CF is a lot harder to predict than the nfl though......in the nfl there ...
    (rec.sport.football.college)