Hi, I need help if I list all the file that determined by user input and if program cannot find file it will display " there no file that you looking for " here is my code but it not working
#!/usr/bin/perl -w
print "Enter Advance Search Function: ";
chomp ($func = <STDIN>);
my @func1 = split(' ', $func);
while ($func ne "quit")
{
if (($func1[0] eq "/") && ($func1[2] eq "/"))
{
foreach $file (glob "$func1[1]")
{
#if ($file)
# {
print "$file\n";
# }
}
if ($file == 0 )
{
print "there no file that you looking for. please Re-Enter\n";
}
}
else
{
print " Incorrect vaiable was entered\n";
}
print "Enter Advance Search Function: ";
chomp ($func = <STDIN>);
}
When compile it list a file but there still error message pop up even there is file in directory
$ perl test3.pl
$ Enter Advance Search Function: / *.pl /
final.pl
ImageMenu_image.pl
test.pl
test2.pl
test3.pl
Use of uninitialized value $file in numeric eq (==) at test3.pl line 16, <STDIN> line 1.
there no specific widthcard was found. please Re-Enter
$ Enter Advance Search Function:
The $file variable is out of scope when you check it. It doesn't exists when the foreach loop ends. Try using an additional array variable.
$ #!/usr/bin/perl -w
print "Enter Advance Search Function: ";
chomp ($func = <STDIN>);
my @func1 = split(' ', $func);
while ($func ne "quit")
{
if (($func1[0] eq "/") && ($func1[2] eq "/"))
{
my @files = glob $func1[1];
if ( ! @files ) {
print "there no file that you looking for. please Re-Enter\n";
}
else {
foreach my $file ( @files ) {
print $file, "\n";
}
}
# foreach $file (glob "$func1[1]")
# {
# #if ($file)
# # {
# print "$file\n";
# # }
# }
# if ($file == 0 )
# {
# print "there no file that you looking for. please Re-Enter\n";
# }
}
else
{
print " Incorrect vaiable was entered\n";
}
print "Enter Advance Search Function: ";
chomp ($func = <STDIN>);
}
And I suppose you will have to reasign user input to the variable @func1 inside the loop, because instead results will always be the same and incorrect.