Writing a Perl Script that processes multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Writing a Perl Script that processes multiple files
# 8  
Old 10-05-2011
Just like that other variable you declared there, you have to declare a 'my $N' and 'my $filename' because you have -w forced on.

---------- Post updated at 12:18 PM ---------- Previous update was at 12:15 PM ----------

That 'd' you deleted meant something:
Code:
$filename=sprintf("/cchome/perm/pedigree/250.1chr%.ped", $N);

Code:
$filename=sprintf("/cchome/perm/pedigree/250.1chr%d.ped", $N);

'%d' means 'substitute integer'. '%.p' means 'print memory pointer', I think.

Last edited by Corona688; 10-05-2011 at 03:26 PM..
This User Gave Thanks to Corona688 For This Post:
# 9  
Old 10-05-2011
Thanks, that partially solved the problem. So I modified my further and now it looks like this:


Code:
#!/usr/local/bin/perl  -w
use Carp;
my @file;


for($N=1; $N<=22; $N++)
{
        $filename=sprintf("250.1chr%d.ped", $N);
        if(!open(FILE, "<${filename}"))
        {
                print STDERR "Couldn't open ${filename}\n";
        }
    else
    {
    my $pheno_tmp = "";
        $pheno_tmp = $filename;

        if ($pheno_tmp =~ /(.+)chr/)
        {
                $pheno_tmp = $1;
        }
    else
    {
                $pheno_tmp = "Undef";
        }
    open (IN,"/cchome/perm/pedigree/output/$filename")   ||
die "cannot open input
file $filename";
        while (<IN>)
        {
    if ($_ =~ /Testing marker:/){chomp($_); my @marker = split(/\s+/,$_); my $marker = $marker[2]; print OUT $pheno_tmp, "\t", $marker,"\t";}   $
        if ($_ =~ /probands/){chomp($_); my @pvalue = split(/\s+/,$_);
        my $Rsq = $pvalue[5]; my $pvalue = $pvalue[7]; print OUT "$Rsq\t$pvalue";print OUT "\n";}      #change the keywords;
        }
    close (IN);

}
close(FILE);
}
close (OUT);

Now I'm getting a diff error message. It says:

Code:
print() on unopened filehandle OUT at getout.pl line 33, <IN> line 120086.
print() on unopened filehandle OUT at getout.pl line 33, <IN> line 120086.
print() on unopened filehandle OUT at getout.pl line 31, <IN> line 120088.
print() on unopened filehandle OUT at getout.pl line 33, <IN> line 120092.
print() on unopened filehandle OUT at getout.pl line 33, <IN> line 120092.
print() on unopened filehandle OUT at getout.pl line 31, <IN> line 120094.
print() on unopened filehandle OUT at getout.pl line 33, <IN> line 120098.
print() on unopened filehandle OUT at getout.pl line 33, <IN> line 120098.
print() on unopened filehandle OUT at getout.pl line 31, <IN> line 120100.
print() on unopened filehandle OUT at getout.pl line 33, <IN> line 120104.
print() on unopened filehandle OUT at getout.pl line 33, <IN> line 120104.

Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!


---------- Post updated at 01:34 PM ---------- Previous update was at 01:27 PM ----------

Actually I solved the problem I think. I needed to open filehandle OUT. Thank you so much Smilie

Last edited by vgersh99; 10-05-2011 at 03:34 PM.. Reason: once again - please use code tags!
# 10  
Old 10-05-2011
You're not closing FILE anywhere. You should close FILE if you succeed in opening it. Otherwise you're opening 22 separate files and leaving them open.

In fact, why not use FILE in the first place instead of reopening it in IN ?
# 11  
Old 10-05-2011
Quote:
Originally Posted by Corona688
I keep forgetting perl doesn't have continue. That's annoying, it has everything else you could think of...
Perl has "next" and "last" instead:

Code:
$
$
$ # "next" skips current iteration and goes on to the next iteration
$
$ perl -le 'for $i (1..10)
           {
             next if $i%4 == 0;
             print $i
           }'
1
2
3
5
6
7
9
10
$
$
$ # "last" skips current iteration and exits the loop altogether
$
$ perl -le 'for $i (1..10)
           {
             last if $i%4 == 0;
             print $i
           }'
1
2
3
$
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to run perl script on multiple files of two directories?

Hi I have 100 files under file A labled 1.txt 2.txt.....100.txt(made up name) I have 1 files under file B labled name.txt How can i run the same perl script on 100 files and file name.txt I want to run perl script.pl A/1.txt B/name.txt perl script.pl A/2.txt B/name.txt ....... perl... (3 Replies)
Discussion started by: grace_shen
3 Replies

2. UNIX for Dummies Questions & Answers

Writing a script to print the number of lines in multiple files

Hi I have 1000 files labelled data1.txt through data1000.txt. I want to write a script that prints out the number of lines in each txt file and outputs it in the following format: Column 1: number of data file (1 through 1000) Column 2: number of lines in the text file Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

3. Shell Programming and Scripting

How can I do one liner import multiple custom .pm files in my perl script?

I am new for Perl I want to ask one question. I have around 50 custom packages which i am using in my Perl script. I want to import all .pm packages in my Perl script in an easy way. Right now i have to import each package individually. So Is there any way to do so?? Right Now i am doing like: ... (1 Reply)
Discussion started by: Navrattan Bansa
1 Replies

4. UNIX for Dummies Questions & Answers

Writing a loop to process multiple input files by a shell script

I have multiple input files that I want to manipulate using a shell script. The files are called 250.1 through 250.1000 but I only want the script to manipulate 250.300 through 250.1000. Before I was using the following script to manipulate the text files: for i in 250.*; do || awk... (4 Replies)
Discussion started by: evelibertine
4 Replies

5. UNIX for Dummies Questions & Answers

Writing a loop to manipulate a script and store it in multiple output files

I have a script where the the 9th line looks like this: $filename=sprintf("250.1chr%d.ped", $N); I want to modify this script 1000 times, changing 250.1chr%d.ped to 250.2chr%d.ped, 250.3chr%.ped.......and so on all the way to 250.1000chr%d.ped and store each output in files called ... (4 Replies)
Discussion started by: evelibertine
4 Replies

6. UNIX for Dummies Questions & Answers

Writing a for loop that processes multiple input files

I would like to write a for loop that does the following: I have a file called X.txt and other files called 1.txt,2.txt, .....,1000.txt. I want to substitute the 6th column of the file X.txt with 1.txt and store the output as X.1. Then I want to do the same with X.txt and 2.txt and store the... (1 Reply)
Discussion started by: evelibertine
1 Replies

7. Shell Programming and Scripting

Run perl script on files in multiple directories

Hi, I want to run a Perl script on multiple files, with same name ("Data.txt") but in different directories (eg : 2010_06_09_A/Data.txt, 2010_06_09_B/Data.txt). I know how to run this perl script on files in the same directory like: for $i in *.txt do perl myscript.pl $i > $i.new... (8 Replies)
Discussion started by: ad23
8 Replies

8. Shell Programming and Scripting

perl script on multiple files

I have a script that runs on one file (at a time). like this: $> perl myscript.pl filename > output How can I run it on >6000 files and have the output sent out into slightly modified file name $> perl myscript 6000files> output6000files.new extension Thanks in anticipation (4 Replies)
Discussion started by: aritakum
4 Replies

9. Shell Programming and Scripting

Multiple processes writing on the same file simultaneously

Hi All, I have encountered a problem,please help me. I have a script in which multiple processes are writing on to the same file . How should I stop this , I mean lock mechanism can be implemented or we can write the at different files and then concatenate the files. What would be a better... (1 Reply)
Discussion started by: Sayantan
1 Replies

10. Shell Programming and Scripting

Combining Multiple files in one in a perl script

All, I want to combine multiple files in one file. Something like what we do on the commad line as follows -> cat file1 file2 file3 > Main_File. Can something like this be done in a perl script very efficiently? Thanks, Rahul. (1 Reply)
Discussion started by: rahulrathod
1 Replies
Login or Register to Ask a Question