Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

next unless - and then 'until'

by Win (Novice)
on Mar 13, 2006 at 15:40 UTC ( #536276=perlquestion: print w/replies, xml ) Need Help??
Win has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I would like the following bit of code
while(my $job_B=readdir(DIR_B)){ print "D\n"; next unless $job_B =~ /Out_$tag\.txt$/; }
to read...
next unless $job_B =~ /Out_$tag\.txt$/; #.... until you have checked t +he whole directory and then goto: beginning.
Sorry that was a bit of perl mixed in with English

Replies are listed 'Best First'.
Re: next unless - and then 'until'
by japhy (Canon) on Mar 13, 2006 at 15:42 UTC
    Do you mean you want a condition that has to be met ONCE and then is "sticky"? Like:
    my $keep_going = 0; while (my $job_B = readdir DIR_B) { print "D\n"; # ||= here means "assign to $keep_going if $keep_going is false" $keep_going ||= $job_B =~ /Out_$tag\.txt$/; next unless $keep_going; ... }

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
      I'm not quite sure what you have written here. However I am fairly sure that it is not what I am looking for. I want the snippit to check each file in the directory but only do that once. After it has check each file in the directory I want it to follow a goto statement. I would be quite interested to hear a description of what you have written in your snippit.
        I want the snippit to check each file in the directory but only do that once. After it has check each file in the directory I want it to follow a goto statement.

        Step back, and explain what you want to accomplish, and forget how think you want the code to work. It sounds like you want to filter a directory based on some condition, then do something with that list. Forget the goto - if you are doing something until something, then just fall out of the bottom of the loop.

        # untested opendir( my $dh, $some_dir ) or die "can't opendir $some_dir: $!"; my @files = grep { ...some condition... } readdir( $dh ); closedir $dh; # there, now do whatever you wanted to `goto` here, # iterating or whatever over @files # ...

        Update: generalized.

        Can you provide wider context? I'm a little confused.

        Steve Marvell

Re: next unless - and then 'until'
by zer (Deacon) on Mar 13, 2006 at 23:24 UTC
    #!/usr/bin/perl -w $tag= "al"; opendir (DIR_B, ".."); $c= 0; {TEMP: while(my $job_B=readdir(DIR_B)){ print "D $c\n"; next unless $job_B =~ /Out_$tag\.txt$/; } } print "DONE\n"; close (DIR_B);opendir (DIR_B, ".."); goto ("TEMP");
    this will scan the directory. once it finishes reading itll close the file handle for a new one or the same or whatever. You need to be more specific but this might be the goto you were looking for
      Just to clarify. I want this program to read the directory with the while statement but only read it once. After it has read it once I want it to use a goto statement to return to the outer most while statement ie.while (1){.

        Yes, and we still have no idea what you are trying to accomplish, nor really enough detail to puzzle out an answer. If you are inside a while, you don't need to goto it. You can either next LABEL to it, or just fall through the bottom of the loop. With no idea what else is in the respective loops, it's extremely hard to be useful.

        # use strict, etc... OUTER: while ( 1 ) { # something1 last OUTER if ...something2...; # something3 INNER: while ( my $dir = readdir( $dh ) ) { # something4 next INNER if ...something5...; # something6 } next OUTER if ...something5...; # something7 }

        Now, if some of the various somethings don't exist, you can simplify greatly. Fill in the somethings with what you want, try it, and if you are still having problems, post the code, the errors, the input and the expected output.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://536276]
Approved by friedo
and all is calm...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2018-06-21 14:45 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (118 votes). Check out past polls.