Need help If empty - perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help If empty - perl
# 1  
Old 09-20-2011
Need help If empty - perl

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

Code:
#!/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
Code:
$ 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:

# 2  
Old 09-20-2011
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.

Code:
$ #!/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.

Regards,
Birei
This User Gave Thanks to birei For This Post:
# 3  
Old 09-20-2011
Thank you so much here is code
Code:
$ #!/usr/bin/perl -w
print "Enter Advance Search Function: ";
chomp ($func = <STDIN>);
while ($func ne "quit")
{
    my @func1 = split(' ', $func);
    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>);

}

Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl , uploading empty file.

Hi The below script used to work fine. Suddenly it's uploading empty file. I am very new to perl. Please help me to find out the problem. #!/usr/bin/perl #script: upload.pl use CGI qw/:standard/; print header, start_html('File upload'); print_form(); print_results() if... (2 Replies)
Discussion started by: Anupam_Halder
2 Replies

2. Programming

Perl - How to empty a directory?

Hi Guys, i'm writing a perl script which whenever runs, should empty 3 pre-decided directories as first step and then the script has the logic to parse some other directories and copy the files inside those directories into these 3 directories. I've the logic already developed, just need to... (2 Replies)
Discussion started by: jhamaks
2 Replies

3. Shell Programming and Scripting

Check if Queue empty or full in perl

Hi, I got problem with queue code how to determined empty and full and problem with while loop Here is my pseudo code : Input page Access Input Pgae Frame For i =3 to pageframe count by 1 construct queue of size i set pageFaultCount to 0 while morepages do page = NextPage... (1 Reply)
Discussion started by: guidely
1 Replies

4. Shell Programming and Scripting

Perl : how to match non-empty string that has no spaces

Hi Everyone, I am looking for neat way to grep a non-empty string that basically contains a hostname, which might be in FWDN form or without the domain, for example: hostname.internal.domainname.net The file I am parsing contains blan lines (^$) and also series of "-" which in other places... (2 Replies)
Discussion started by: togr
2 Replies

5. Shell Programming and Scripting

perl or awk remove empty lines when condition

Hi Everyone, # cat 1 a b b cc 1 2 3 3 3 4 55 5 a b (2 Replies)
Discussion started by: jimmy_y
2 Replies

6. Shell Programming and Scripting

perl script to check if empty files are created and delete them and run a shell script

I have a local linux machine in which the files are dumped by a remote ubuntu server. If the process in remote server has any problem then empty files are created in local machine. Is there any way using perl script to check if the empty files are being created and delete them and then run a shell... (2 Replies)
Discussion started by: hussa1n
2 Replies

7. UNIX for Dummies Questions & Answers

Getting same exit status for empty and non empty file

Hi All, I am checking for a empty input file to do some further action , but I am getting exit status 0 in both the cases , for empty and non empty file both. The value of $? is coming 0 in if part also and else part too. #!/bin/ksh if ]; then echo "data" # exit 0 echo "$?" else... (4 Replies)
Discussion started by: mavesum
4 Replies

8. UNIX for Dummies Questions & Answers

How to check for empty file in Perl?

Hi, May I know how to check for empty file in Perl. Iam missing something, somewhere. #!/usr/bin/perl my $open_dir = '/path/'; my $file; my $ma = "abc_.*.\.psv\$" opendir(VAR, $open_dir) or die "Can't open $oepn_dir: $!\n"; while( defined ($file = readdir VAR) ) #read all... (1 Reply)
Discussion started by: deepakwins
1 Replies

9. UNIX for Dummies Questions & Answers

how to find empty folders without using -empty

hi all: my solaris FIND does not support find myFolder -type d -empty how can i find the empty folders? thanks! (7 Replies)
Discussion started by: lasse
7 Replies

10. Shell Programming and Scripting

printing an empty line in a file (perl)

I know this must be really easy, but i can't get it to work I've got a perl script, with a file. I want to print an empty line, and the following doesn't seem to work: print nameoffile "\n" thanks for your help!! (3 Replies)
Discussion started by: kfad
3 Replies
Login or Register to Ask a Question