Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Nested Foreach Loop skipping every other element??

by ba1688 (Novice)
on Feb 19, 2014 at 16:15 UTC ( [id://1075478]=perlquestion: print w/replies, xml ) Need Help??

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

So I have a nested for each loop
use strict; use warnings; use File::Copy; my @files = glob("*.hml"); my @DIR = glob ("*/*.hml"); my @array = ('', grep -d, glob '*'); print "Here's DIR @DIR"; my $argsucess = 0; foreach my $el (@ARGV){ print "\n\n\nheres el $el"; my $delele=0; foreach my $element (@DIR){ print "\n\n\nHERE'S ELE $element"; print "\n\n\n\n\n\n HERE'S DIR SO FAR @DIR\n\n\n"; my $re1='((?:[a-z][a-z]+))'; # Non-greedy match on filler my $re=$re1; my $path; if ($element =~m/$re/is){ $path = $1; } if ($el eq $path){ splice(@DIR, $delele, 1); $argsucess++; next; } $delele++; } }
All I'm trying to make this do splice out the files in the directory I specify. It's doing it but for some reason it's doing it to every other element in the directory. For example if I have @DIR= functions/myfunc.hml perltest/abs.hml perltest/abs3.hml perltest/abs4.hml perltest/acos2.hml perltest/zeros17.hml It finds myfunc abs abs4 and zeros17 yet abs3 and acos2 are in the specified directory. It splices out the abs and abs3 yet leaves everything else in that directory. I need it to delete the array elements in @DIR that contain the "perltest" for example. Somethings wrong but I can't see what.

Replies are listed 'Best First'.
Re: Nested Foreach Loop skipping every other element??
by hdb (Monsignor) on Feb 19, 2014 at 16:36 UTC
      So should how else can I delete that element??

        Instead of deleting the unwanted ones, you could push the wanted ones onto a new list. Or try to rewrite your conditions into a grep statement.

        Instead doing splice on the same @DIR which you used in the foreach, you can have a copy of @DIR and you can do splice on that array. For Ex. @DIR will be used in foreach, @DIR_COPY can be used in splice.


        All is well
Re: Nested Foreach Loop skipping every other element??
by Kenosis (Priest) on Feb 19, 2014 at 16:58 UTC

    Seems grepping, as hdb suggested, may produce your desired results:

    use strict; use warnings; my @DIR = qw{functions/myfunc.hml perltest/abs.hml perltest/abs3.hml p +erltest/abs4.hml perltest/acos2.hml perltest/zeros17.hml}; my @filtered = grep !m{^perltest/}, @DIR; print "$_\n" for @filtered;

    Output:

    functions/myfunc.hml
Re: Nested Foreach Loop skipping every other element??
by kennethk (Abbot) on Feb 19, 2014 at 17:44 UTC
    After considering hdb and Kenosis's advice, when I need to iterate over an array I am dropping terms from, I usually use an explicit index and iterate backwards over the indices. Note that you already need to use $delele (which I assume should be $delete) to track the index. As long as you are already tracking it, you might as well include that in your control structure. So maybe your code would read (untested):
    use strict; use warnings; use File::Copy; my @files = glob("*.hml"); my @DIR = glob ("*/*.hml"); my @array = ('', grep -d, glob '*'); print "Here's DIR @DIR"; my $argsucess = 0; foreach my $el (@ARGV){ print "\n\n\nheres el $el"; foreach my $delele (reverse 0 .. $#DIR){ my $element = $DIR[$delele]; print "\n\n\nHERE'S ELE $element"; print "\n\n\n\n\n\n HERE'S DIR SO FAR @DIR\n\n\n"; my $re1='((?:[a-z][a-z]+))'; # Non-greedy match on filler my $re=$re1; my $path; if ($element =~m/$re/is){ $path = $1; } if ($el eq $path){ splice(@DIR, $delele, 1); $argsucess++; next; } } }
    The benefit to reverse traversal is that you don't interfere with the indices for future iterations by splicing this one out.

    You might also consider using qr// for your regular expression (see Regexp Quote Like Operators). I'm a little unclear on why you aren't just using a fixed regular expression in if ($element =~m/$re/is){, nor why you are using the my $re=$re1; misdirection. Also, by initializing my $path; to undef instead of empty string (my $path = '';), you will likely get a number of unnecessary warnings from if ($el eq $path){.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      my $re1='((?:[a-z][a-z]+))'; # Non-greedy match on filler

      ba1688: Also be aware that the quoted regex (also in the OP) is not "non-greedy" either by its definition or by virtue of the way it is used (Update: well, not in any regex-related sense, anyway).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-26 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found