For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > February 2005 > Calling Files automatically









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Calling Files automatically
Edward Wijaya

2005-02-25, 3:57 pm

Hi,

How can I modify the early part of code below
such that I don't have to manually key in the files this way:

perl mycode.pl data1P.fa data1R.fa data2P.fa data2R.fa ....etc

I.e. just by typing this will do:

perl mycode.pl


Thanks so much for your time beforehand.

--
Edward WIJAYA
Singapore

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

for (@ARGV) {
next if /P\./; # ignore the P files
my ($base, $ext)= split /R/; #assume only one 'R' in the file name
r_p_process ( $base."R".$ext, $base."P".$ext )
}

sub r_p_process {
my $r_file=shift;
my $p_file=shift;
print "processing the pair $r_file $p_file\n";

#then process two files
}
__END__



NOTE:
The code above take pair-series of files as follows:

data1R.fa - data1P.fa
data2R.fa - data2P.fa

From list of files in directory

data1R.fa
data2P.fa
data2R.fa
data2P.fa
..... #and there are more than 40 of these type files

Charles K. Clarkson

2005-02-25, 3:57 pm

Edward Wijaya <ewijaya@singnet.com.sg> wrote:

: How can I modify the early part of code below such that
: I don't have to manually key in the files this way:
:
: perl mycode.pl data1P.fa data1R.fa data2P.fa data2R.fa
: ....etc
:
: [snip]
:
: NOTE:
: The code above take pair-series of files as follows:
:
: data1R.fa - data1P.fa
: data2R.fa - data2P.fa
:
: From list of files in directory
:
: data1R.fa
: data2P.fa
: data2R.fa
: data2P.fa
: .... #and there are more than 40 of these type files

Is it always data1, data2, data3, etc. or can data be
another word? (Like foo1, foo2, foo3, etc.)

Is the extension always .fa or can it be something else?

Will every R file have a matching P file or will
some extra files be located in the directory? For example,
will there sometimes be a data14R.fa file and no
corresponding data14P.fa file?


Will there be other files located in this directory?


Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328


Wiggins d'Anconia

2005-02-25, 3:57 pm

Edward Wijaya wrote:
> Hi,
>
> How can I modify the early part of code below
> such that I don't have to manually key in the files this way:
>
> perl mycode.pl data1P.fa data1R.fa data2P.fa data2R.fa ....etc
>
> I.e. just by typing this will do:
>
> perl mycode.pl
>
>
> Thanks so much for your time beforehand.
>


Based on the comment in the code the file list is in a directory? If so,

perldoc -f opendir
perldoc -f readdir
perldoc -f glob

Should provide some obvious solutions.

http://danconia.org
Edward WIJAYA

2005-02-26, 3:55 am

Thanks for your reply Charles,

On Fri, 25 Feb 2005 09:10:54 -0600, Charles K. Clarkson
<cclarkson@htcomp.net> wrote:

> Is it always data1, data2, data3, etc. or can data be
> another word? (Like foo1, foo2, foo3, etc.)
>


it always comes in (R-P) pair:
data1R.fa
data1P.fa
foo1R.fa
foo1P.fa
bar2R.fa
bar2P.fa


So "data1R.fa" is always processed with "data1P.fa", and so forth.


> Is the extension always .fa or can it be something else?


Yes the extension is always ".fa"

>
> Will every R file have a matching P file or will
> some extra files be located in the directory? For example,
> will there sometimes be a data14R.fa file and no
> corresponding data14P.fa file?


No, there will always be a corresponding file.
i.e. *R.fa always have its *P.fa file

>
>
> Will there be other files located in this directory?


Yes, but they won't be any other *R.fa *P.fa type.

>
>
> Charles K. Clarkson




--
Regards,
Edward WIJAYA
Singapore
Charles K. Clarkson

2005-02-26, 3:55 am

Edward WIJAYA <ewijaya@singnet.com.sg> wrote:

: On Fri, 25 Feb 2005 09:10:54 -0600, Charles K. Clarkson
: <cclarkson@htcomp.net> wrote:
:
: : Is it always data1, data2, data3, etc. or can data be
: : another word? (Like foo1, foo2, foo3, etc.)
: :
:
: it always comes in (R-P) pair:
: data1R.fa
: data1P.fa
: foo1R.fa
: foo1P.fa
: bar2R.fa
: bar2P.fa
:
:
: So "data1R.fa" is always processed with "data1P.fa", and so
: forth.
:
:
: : Is the extension always .fa or can it be something else?
:
: Yes the extension is always ".fa"

Then the original script could be rewritten to this. Only
the "R" files needed to be supplied.

# ARGV need only contain "R" files.
foreach my $r_file ( @ARGV ) {

next unless $r_file =~ /(.*)R.fa$/;

r_p_process( $r_file, "$1P.fa" );
}


: : Will every R file have a matching P file or will
: : some extra files be located in the directory? For example,
: : will there sometimes be a data14R.fa file and no
: : corresponding data14P.fa file?
:
: No, there will always be a corresponding file.
: i.e. *R.fa always have its *P.fa file
:
: :
: :
: : Will there be other files located in this directory?
:
: Yes, but they won't be any other *R.fa *P.fa type.

Look up the use of the 'glob' function included in perl.
The perlfunc file has info on it. The angle brackets act as a
shortcut. Something like <*R.fa> should point you in the right
direction.


HTH,

Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328










Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com