Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

loop issue...

by lomSpace (Scribe)
on Mar 20, 2009 at 14:34 UTC ( #752054=perlquestion: print w/replies, xml ) Need Help??

lomSpace has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am trying to resolve a looping issue which causes my script
to append the files 4x instead of once.
#!/usr/bin/perl use strict; use Data::Dumper; use File::Glob ':glob'; my @dirs = glob("C:/Documents and Settings/mydir/Desktop/KOMP/*"); foreach my $maid_dir(@dirs){ if (-d $maid_dir){ # directory check if ($maid_dir=~m%\d+\s[A-Z]%){ # match the dir name my $seq_dir = $maid_dir."/sequencing/"; chdir ($maid_dir); #next if (-M $seq_dir > 1.0); chdir ($seq_dir); #print Dumper ($maid_dir, $seq_dir); opendir my $dh, "$seq_dir"; if(-d $seq_dir){ my @files = readdir $dh; for my $f(@files){ if($f=~ m%^(\d*)(HU.fa|HD.fa|Ltvec_small.FA|_fasta +)$%){ my ($fa, $hu, $hd, $lt)= ($1."_fasta", $1."HU. +fa", $1."HD.fa", $1."Ltvec_small.FA"); if(-e $fa){ open (my $out, ">>".$seq_dir."/".$fa) or d +ie "Can't open $seq_dir/$fa for append: $!\n"; # Append the contents of the three files t +o $fa open my $hu_file, '<', $hu or die "Can't o +pen $hu: $!\n"; while (my $line = <$hu_file>) { print $out $line; } open my $hd_file, '<', $hd or die "Can't o +pen $hd: $!\n"; while (my $line = <$hd_file>) { print $out $line; } open my $lt_file, '<', $lt or die "Can't o +pen $lt: $!\n"; while (my $line = <$lt_file>) { print $out $line; } print $out, "\n"; #print Dumper($f); close($out); close($hu_file); close($hd_file); close($lt_file); } } } } } } }
Any ideas?

Replies are listed 'Best First'.
Re: loop issue...
by bellaire (Hermit) on Mar 20, 2009 at 14:40 UTC
    Well, here you check whether this particular file is one of the four you want:
    for my $f(@files){ if($f=~ m%^(\d*)(HU.fa|HD.fa|Ltvec_small.FA|_fasta)$%) {
    But on the very next line, you decide to process all of them:
    my ($fa, $hu, $hd, $lt)= ($1."_fasta", $1."HU.fa", $1."HD.fa", $1."Ltv +ec_small.FA");
    So each time your code hits HU.fa, it'll process all the files. Then when it hits HD.fa, it'll process them all again. Same for the other two. That's why you see the output 4x.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: loop issue...
by kennethk (Abbot) on Mar 20, 2009 at 14:44 UTC
    As a side note, you may want to add or die clauses after your chdir, opendir etc. And you should probably swap open $out to a three-argument form like you have for your other open statements.

      Sounds like awfully familiar advice, doesn't it OP? :)

      He's at least checking your open calls which is good, but he'll be kicking himself n months hence when he has to spend time chasing down why things don't work that a simple opendir( FOO, $dir ) or die "opendir '$dir': $!" would have caught.

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        The OP copied the lines for the open calls that I provided -- but skipped the rest of the script. Granted I missed the check on the readdir, but I also noted that (and at least failure there would just mean that @files would be empty, rather than trying to alter files in the wrong directory).

        I had hoped that the OP would see what was intended with the advice given, but I think that's too much to ask. I was toying with the idea of completely revising the script (I have a current script that's taking a very long time to execute, so have some downtime while I wait for it) but I think it would be rather pointless.

        I got the right output. Thanks!
      ...or, alternatively, use autoload; autodie; - which would have the effect of die()ing if a built-in fails, thus not unduly cluttering the code.

      Update:

      Thanx to Fletch, I did in fact mean autodie

      A user level that continues to overstate my experience :-))
      Thanks for the help. I got the right output!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://752054]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2023-01-31 17:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?