Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

foreach loop running too many times...?

by fasoli (Beadle)
on Feb 10, 2016 at 20:18 UTC ( [id://1154892]=perlquestion: print w/replies, xml ) Need Help??

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

Hello dear monks! I've been driving myself crazy over the code below, I can't figure out how to make it run properly :( The files that I'm trying to match are in the style of AC0001_F00001_S00001.tpr, AC0002_F00002_S00001.tpr, AC0003_F00003_S00001.tpr etc. AC goes from 1 to 50 (so AC0001 to AC0050) and frames go from F00001 to F00020.

So what I'm doing is: I'm putting all my tpr files in an array, then I match their names and then I want to say "for molecule 1 to 2" and "for frame 1 to 2" -> print what files you are finding. However what happens is that, because of the foreach loop, the script print the files that it finds 6 times in total, which is exactly how many tpr files I have. I have tried closing the foreach loop earlier, before the for loops, but that makes no sense. Is there a syntax error? Or am I using the loops totally wrong?

#!/usr/bin/perl/ use strict; use warnings; my $tpr = "tpr"; my $id; my $filepath; my $batch; my $number; my $molec; my $frame; my $mdstep; my @tpr; @tpr = `ls *.tpr`; print "Script start \n"; print "\n"; foreach (@tpr) { /(\w{2})(\d{4})_F(\d{5})_S(\d{5})/; $id = "AC"; $molec = $2; $frame = $3; $mdstep = $4; for ($molec=2; $molec<=2; $molec++) { for ($frame=1; $frame<=2; $frame++) { my $molecform = sprintf ("%04d", $molec); my $frameform = sprintf ("%05d", $frame); print "$id${molecform}_F${frameform}_S${mdstep}\.$tpr \n"; } } } # end of loop through tpr files

Replies are listed 'Best First'.
Re: foreach loop running too many times...?
by choroba (Cardinal) on Feb 10, 2016 at 20:23 UTC
    You assign capture groups to $molec and $frame, but never use the assigned values. Instead, you change their values in the loops.

    Unrelated comments:

    • Declaring all the variables at the start is needed in Pascal, not Perl. Use the smallest possible scope.
    • No need to call ls:
      @tpr = `ls *.tpr`;

      could be written in a more Perlish way as

      @tpr = glob '*.tpr';
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: foreach loop running too many times...?
by poj (Abbot) on Feb 10, 2016 at 20:51 UTC

    Match the values you want with a regex

    #!perl use strict; my @tpr = qw( AC0001_F00001_S00011.tpr AC0001_F00002_S00012.tpr AC0001_F00003_S00013.tpr AC0002_F00001_S00021.tpr AC0002_F00002_S00022.tpr AC0002_F00003_S00023.tpr AC0003_F00001_S00031.tpr AC0003_F00002_S00032.tpr AC0003_F00003_S00033.tpr); foreach (@tpr) { if (/(\w{2})(\d{4})_F(\d{5})_S(\d{5})/){ my $id = "AC"; my $molec = $2; my $frame = $3; my $mdstep = $4; # molecule 1 to 2 and frame 1 to 2 if ( ($molec =~ /000[12]/) && ($frame =~ /0000[12]/) ){ print "$_\n"; } } } # end of loop through tpr files
    poj

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2024-04-18 12:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found