50 users online (0 members and 50 guests)  


  Results 1 to 2 of 2

Related

  1. Meaning of dis perl script    Forum: CGI Perl Forum
    Replies: 1
  2. Please Help me (Perl Script)    Forum: CGI Perl Forum
    Replies: 1
  3. Executing the PERL CGI Script    Forum: CGI Perl Forum
    Replies: 1
  4. Perl script error..    Forum: CGI Perl Forum
    Replies: 1
  5. cgi script/perl problem    Forum: CGI Perl Forum
    Replies: 4
  1. #1
    pleonardd's Avatar
    New User

    Status
    Offline
    Join Date
    Nov 2007
    Posts
    1

    Help with my perl script

    I am new to using perl and am stuck with the last piece of my script and I think it should be fairly easy but I can't get the syntax correct. What I am doing is writing data to a text file. What I need to do is to determin if there is more than one line of text in the file, and if so to perform a system function. If there is no more than one line in the file to end the script.
    Any help would be appreciated. This is what i have so far:
    open(FH,'yourfile.txt') or die "$!";
    while(<FH>) {
    if ($. > 1) {
    print "The file has more than one line\n";
    last;
    }
    }
    close FH;

  2. #2
    xmassey's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Apr 2007
    Posts
    100

    Re: Help with my perl script

    Code:
    #Open text file into array @log
    open (LOG, "<yourfile.txt") || die "D";
    flock (LOG, 2) || die "D";
    my @log = <LOG>;
    close (LOG) || die "D";
     
    #Count the number of lines in the text file
    my $line_count = @log;
     
    #If there is more than one line in the text file
    if ($line_count > "1") {
         print "This file has more than 1 line\n"
    }
    Not sure why your script fails. I have never used $. so that might be the problem, and I haven't looked up what it does. But I am assuming it automatically counts the number of lines in a file? I have simply counted the number of items in the array @log (each item per line).
    Chris



Tags for this Thread