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

My IF Statement doesnt work. It prints out "$image $input does not exist" all on the same page. how do I get it to print an error string if File::Find::Rule cannot find the $image in $imageDir? thanks
my $input = $q->param('input'); my $Num =~ m/^\d{6,6}\.jpg$/i; chdir('/path/to/images') or die $!; @files = File::Find::Rule->file() ->name(qr{$Num}) ->in($imageDir); print $q->header(); foreach my $image (@files){ unless ($input){ next; } if($image =~ /$input/){ print $image; } else{ print "$input does not exist!\n"; } }

Replies are listed 'Best First'.
Re: if statement doesnt work
by RazorbladeBidet (Friar) on Feb 11, 2005 at 16:20 UTC
    Try and use some indentation first off, and make sure you're use-ing strict;

    But it looks like you're saying the $input doesn't exist if you can't find it ONCE. If you have 10 files and you hit the first file and it doesn't match, are you going to say it doesn't exist?

    What you might want is this:

    use strict; ... my $found = 0; if ( $input ) { foreach my $image ( @files ) { # are you just looking to see if image CONTAINS input? if ( $image =~ /$input/ ) { $found = 1; last; } } } print "$input ".($found ? "found" : "not found")."\n";


    HTH
      Yes..am checking if image contains input. Your solution works. thanks :)
Re: if statement doesnt work
by gellyfish (Monsignor) on Feb 11, 2005 at 16:17 UTC

    Yes because you are doing it in a foreach loop. Put last; after your print $image if you don't want that to happen.

    /J\