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

I have the following script that prints files names for the given folder.
my $directory = ".\\folder"; opendir (DIR, $directory) or die $!; while (my $fldr = readdir(DIR)) { if ($fldr =~ m/log?/){ print $fldr, "\n"; } }
OUTPUT
(10.10.10.1).log (10.10.10.2).log .................. .................. Press any key to continue . . .
I am only interested in the first file. How can I print the first match only that ends with .log? Tried with $fldr[0], didn't work. Tried, m/log?/... thinking that ? symbol will trigger not to be greedy, but still didn't work.

Please let me know.

UPDATED: Script & output.

Replies are listed 'Best First'.
Re: regex 1st match only
by davido (Cardinal) on Oct 28, 2014 at 16:55 UTC

    That code doesn't compile.

    Assuming it did compile, one way to do what you're asking is this:

    while( CONDITION_A ) { if( CONDITION_B ) { # DO SOMETHING last; # Exits loop immediately. } }

    The last keyword is discussed in perlsyn and last. Some languages would call it break.

    While I'm not among them, there are purists of structured programming who would advise against such short circuits as next, and last. They would prefer you jump through hoops such as this:

    my $found = 0; while( CONDITION_A && ! $found ) { if( CONDITION_B ) { # DO_SOMETHING $found = 1; } else { # BLAH BLAH } }

    In specific cases, such hoops may be able to be rendered less wordy. Either way, it's all just a matter of programming. ;)


    Dave

      While I'm not among them, there are purists of structured programming who would advise against such short circuits as next, and last.

      Those purists probably don't really understand what structured programming is really about and want to make it a bureaucratic straight jacket.

      I remember that one of my CS professors once chastised me for having written a function or procedure (I can't remember which language it was, most probably either C or Pascal) in which I was returning early out of the function if a certain condition was met (or not met, whatever), saying me that this was a form of hidden goto (he had introduced Dijkstra's famous "Goto considered harmful" piece just a week or two before). I still received a fairly good mark, but I really felt at the time it was a ridiculous point.

      I was quite happy, years later, to read somewhere in the Camel Book that programming is often building a decision tree and that it makes sense to discard early special cases, rather than having a deeply nested if/then/elsif/else conditional. Quite often, using next or last can also improve significantly the performance and the readability.

      I would strongly suspect that Perl is not the right language for such purists. Let them write ten times more code-lines with A*a or Ja*a. (OK, sorry, I don't want to introduce a language flame war, but I replaced yesterday a 1500++ lines shell script with less than 20 lines Perl script, and it works better).

        Our objective usually is to develop correct code at a minimum lifecycle cost. Structured programming is a great tool. It should never be allowed to interfere with the objective.
        Bill
Re: regex 1st match only
by ww (Archbishop) on Oct 28, 2014 at 16:18 UTC
    Please use Search or Super Search ... each of which has answers to your question.

    And when you ask another, please note that posting code that shows no attempt to answer your own question really doesn't count for much in terms of demonstrating that you're not merely asking us to do your (home, $$$) -work for you.


    ++$anecdote ne $data