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

I have the following script. It almost works. I am searching through a file, however when reading the file it seems to skip to every second line. I know its something simple, however have not found anything that works. Below is the code and following it would be what the file looks like. Each server would be on a separate line in the text file. Any ideas? CODE**
#!/usr/bin/perl -s use strict; open (FILEHANDLE, "<servers.txt"); while (<FILEHANDLE>) { my $ServerName = <FILEHANDLE>; $ServerName = substr($ServerName, 0, -1); print "\n"; print "Checking $ServerName..........\n"; my $ServerSpaceC = `cmd.exe /c dir "\\\\\"$ServerName"\\c\$" | find /i + "bytes free"`; my $ServerSpaceD = `cmd.exe /c dir "\\\\\"$ServerName"\\d\$" | find /i + "bytes free"`; print "*********** $ServerName : Space Available *************\n\n"; $ServerSpaceC = substr($ServerSpaceC, 25); $ServerSpaceC = substr($ServerSpaceC, 0, - 11); $ServerSpaceC =~ s/,//gi; $ServerSpaceD = substr($ServerSpaceD, 25); $ServerSpaceD = substr($ServerSpaceD, 0, - 11); $ServerSpaceD =~ s/,//gi; my $availServerSpaceC = $ServerSpaceC / 1024 / 1024 / 1024; my $availServerSpaceD = $ServerSpaceD / 1024 / 1024 / 1024; if ($ServerSpaceC <= 999999999) { $availServerSpaceC = $availServerSpaceC * 1024; print "$ServerName C: Drive: $availServerSpaceC MB Free Space\n"; print " Please clean up D: Drive space on $ServerName\n\n"; } else { print "$ServerName C: Drive: $availServerSpaceC GB Free Space\n"; } if ($ServerSpaceD <= 999999999) { $availServerSpaceD = $availServerSpaceD * 1024; print "$ServerName D: Drive: $availServerSpaceD MB Free Space\n"; print " Please clean up D: Drive space on $ServerName\n\n"; } else { print "$ServerName D: Drive: $availServerSpaceD GB Free Space\n"; } print "\n"; print "************ End $ServerName : Space Report *****************\n +\n"; } close(FILEHANDLE); ****Test File server1234 server2345 server5566
**etc

Replies are listed 'Best First'.
Re: Substring problem
by bv (Friar) on Aug 26, 2009 at 19:27 UTC

    Your problem is here:

    while (<FILEHANDLE>) { my $ServerName = <FILEHANDLE>;

    The while(<FILEHANDLE>) reads a value and puts it in $_. Then the next line reads another value and puts it in $ServerName. Try this instead:

    while (my $ServerName=<FILEHANDLE>) {
    $,=' ';$\=',';$_=[qw,Just another Perl hacker,];print@$_;
      Thanks. Is there a way to determine the end of the file? I added a extra return and the end of the file as a workaround...however initially, the while loop would not read the last line. ie..
      server1234 server3444 server5555
      The output would read the first two lines, however the last line server5555 would just display as a blank, until i added a return to the end of the file.

        No need to. The <> operator knows to stop on EOF. You had the problem before because each iteration of the loop had two read ops, so you needed an even number of lines in the file to get it to terminate.

        $,=' ';$\=',';$_=[qw,Just another Perl hacker,];print@$_;
Re: Substring problem
by Fletch (Bishop) on Aug 26, 2009 at 19:28 UTC

    You're reading from your filehandle twice each time through the loop. Don't do that.

    while (<FILEHANDLE>) { my $ServerName = <FILEHANDLE>;

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.