Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Help: Process many files at the same time

Reply
Thread Tools

Help: Process many files at the same time

 
 
Amy Lee
Guest
Posts: n/a
 
      09-30-2008
Hello,

Here's my codes:

foreach $file (@ARGV)
{
while (<>)
{
chomp;
if (/(\d+)\s+dG = (-?[\d.]+)\s+(.*)/)
{
$total = $1;
$name = $3;
s/.*//;
}
$g += tr/C//;
$c += tr/G//;
}
$gc = $g+$c;
$GC = $gc/$total;
print "$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.

I don't know what's errors in there.

Could you tell me what's going on?

Thanks.

Amy
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      09-30-2008
Amy Lee <(E-Mail Removed)> wrote:
>Hello,
>
>Here's my codes:
>
>foreach $file (@ARGV)


Ok, you are looping through all command line arguments

>{
> while (<>)


For each of those arguments you are reading lines from the keyboard
until the user submits an EOF.

Is that what you want?

jue
 
Reply With Quote
 
 
 
 
Amy Lee
Guest
Posts: n/a
 
      09-30-2008
On Mon, 29 Sep 2008 20:44:09 -0700, Jürgen Exner wrote:

> Amy Lee <(E-Mail Removed)> wrote:
>>Hello,
>>
>>Here's my codes:
>>
>>foreach $file (@ARGV)

>
> Ok, you are looping through all command line arguments
>
>>{
>> while (<>)

>
> For each of those arguments you are reading lines from the keyboard
> until the user submits an EOF.
>
> Is that what you want?
>
> jue

Thanks. the codes
while (<>)
{
chomp;
if (/(\d+)\s+dG = (-?[\d.]+)\s+(.*)/)
{
$total = $1;
$name = $3;
s/.*//;
}
$g += tr/C//;
$c += tr/G//;
}
$gc = $g+$c;
$GC = $gc/$total;
print "$name\t$GC\n";
can process one file per time and work well. So I suppose that I can make
a foreach loop to process many files reads from filenames. But it seems
not work. So I wonder what's wrong with the codes.

Could you tell me how to modify that?

Amy
 
Reply With Quote
 
Martien Verbruggen
Guest
Posts: n/a
 
      09-30-2008
On Tue, 30 Sep 2008 11:26:09 +0800,
Amy Lee <(E-Mail Removed)> wrote:
> Hello,
>
> Here's my codes:
>
> foreach $file (@ARGV)
> {
> while (<>)
> {


If you want to read all files on the command line, one after the other,
simply use

while (<>) { }

There's no need to process @ARGV yourself for that.

See the perlop documentation. Look for the section starting with

The null filehandle <> is special: it can be used to emulate the behav‐
ior of sed and awk

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


I don't believe this. Even with the useless foreach loop around the
while loop, all files on the command line still should have been
processed.

This:


#!/usr/bin/perl
use strict;
use warnings;

foreach my $file (@ARGV)
{
while (<>)
{
print;
}
}

Works fine for me, and simply prints the contents of every file I give
it on the command line to STDOUT. It does this on the FIRST iteration of
the foreach loop. After this first iteration, @ARGV is empty, so there
is no second iteration. Since you're not supposed to modify arrays while
you're iterating over them, I suppose that this is current behaviour,
and not necessarily guaranteed. in other words: Don't do it. Either
iterate over @ARGV and handle the files yourself, or use while(<>), and
let it iterate.

Martien
--
|
Martien Verbruggen | Make it idiot proof and someone will make a
| better idiot.
|
 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      09-30-2008
Amy Lee <(E-Mail Removed)> 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/"
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      10-04-2008
On Tue, 30 Sep 2008 08:57:35 -0500, Tad J McClellan <(E-Mail Removed)> wrote:

>Amy Lee <(E-Mail Removed)> 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.


You are the atypical prototype ball buster ain't ya?

sln
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Launching a process (exe) using the same process id as the launcher franckspike C++ 1 07-03-2008 10:30 PM
Is time.time() < time.time() always true? flamesrock Python 8 11-24-2006 06:51 AM
Winzip: which of the many many files do I click to install? dt4emails-websites2@yahoo.com Computer Support 3 11-11-2005 04:45 AM
many aspnet applications in one site? many Gloabal.asax files ok? dee ASP .Net 2 08-15-2005 03:07 AM
Recieving many many copies of the same emails Bill Schowengerdt Computer Support 4 10-29-2003 11:39 AM



Advertisments