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

Hi Monks,
I am trying to search for a specific file name in a directory and if the file is found I want my program to do exit the loop.
No luck cause it does the foreach loop but it finds the file but also gives me back the other files, my search has to be more specific, any help?
Thanks
Here is what I have

opendir(DIR,$img_dir); my @files = readdir(DIR); closedir(DIR); foreach(@files){ if($_ eq $img_name){exit;}else{ print $_,"\n";} }

Replies are listed 'Best First'.
Re: Search Directory
by friedo (Prior) on Feb 01, 2005 at 18:17 UTC
    If you want to exit a loop, use last instead of exit, which exits your program. The following should do what you want, if I understand your question properly.

    foreach(@files){ last if $_ eq $img_name; print $_,"\n"; }

    If you need to do more complex or recursive searching, check out File::Find.

    Update: Fixed typo. (Thanks Grundle :) )

      Your code has an extra curly brace (bug) :p but your analysis of the problem is correct. To explain a little further when you use the last command it will set the position pointer of the array you are traversing to the last entry, which of course means that the array has been fully "traversed" or that there are no more elements to look at.

      The code without the bug would be

      foreach(@files){ if($_ eq $img_name){ last; } print "$_\n"; }
      I hope that helps, good luck
        The code still does the line print "$_\n";
        even if if($_ eq $img_name) this condition is true.
        I would like the code to exit the foreach completely.
        Thanks.
Re: Search Directory
by Grygonos (Chaplain) on Feb 01, 2005 at 18:51 UTC

    Tested & working

    #!/Perl/bin/perl use strict; use warnings; my $dir = 'c:/'; my $file = 'autoexec.bat'; opendir(DIR,$dir); print "found" if grep /$file/,readdir(DIR) ; closedir(DIR);
    so you can adapt that line to last if grep /$file/,readdir(DIR) ; to make your loop exit early

      Hi,
      Getting this back:

      Software error: Can't "last" outside a loop block

      Anyone knows why?

        yes, its a very desriptive error. It means you tried to cut and paste the last statement i provided into your program and didn't put that statement inside a loop construct.. so perl says "Last?!? ... last what?"

Re: Search Directory
by gopalr (Priest) on Feb 02, 2005 at 05:10 UTC

    Hi,

    Use File::Find module to find the file in a directory.

    @ARGV = 'c:/temp/'; use File::Find (); sub find(&@) { &File::Find::find } *name = *File::Find::name; find { if ((-f $name) && ($name=~m#^.+/\QFile.txt\E$#i)) { print "\nFile.txt Found.\n\nExited !!!"; exit; } } @ARGV;

    Thanks,

    Gopal.R

Re: Search Directory
by radiantmatrix (Parson) on Feb 01, 2005 at 21:21 UTC

    Read up on File::Find -- it will probably be the most portable and bug-free way to go; and, it just might be the most efficient to code (depending).

    If your issue is that you only want to find the first file and then stop, try using

    foreach (@files) { next unless ($_ eq $img_name); ## skip ones we don't want print "$_\n"; last; } ## or, if you want all files up to (and including) that name: foreach (@file) { print "$_\n"; last if ($_ eq $img_name); }

    I suggest (re-)reading the "Control Structures" part of the Camel book for more.

    radiantmatrix
    require General::Disclaimer;
    s//2fde04abe76c036c9074586c1/; while(m/(.)/g){print substr(' ,JPacehklnorstu',hex($1),1)}

Re: Search Directory
by blazar (Canon) on Feb 01, 2005 at 19:28 UTC
    Even if this is a properly trimmed down example, I feel smell of XY problem here. What is the point of cyclying until you find a specific name and then printing it, if you know it in the first place?!?

    You may try

    -e "$img_dir/$_" and print for $img_name;
    instead.
Re: Search Directory
by sh1tn (Priest) on Feb 01, 2005 at 19:12 UTC
    SEARCH: { grep { -f and /^$img_name$/ and last SEARCH or print } glob +"$img_dir/*" }