Help with PERL loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with PERL loop
# 1  
Old 10-10-2010
Help with PERL loop

I wrote a script to list all lines in a file with Perl. I am having trouble with the looping part of it. My script is supposed to look at the file and as long as the file is larger than the current line it prints a new line.

I am getting an error that won't stop on the while line of my code I believe.
Here is the code:

perl code:
  1. #!/usr/bin/perl -w
  2. unless(scalar(@ARGV)==1){
  3.    print "error: incorrect number of arguments",
  4.    "\n",
  5.    "usage: linenum.pl [filename]",
  6.    "\n";
  7.       exit 1;
  8. }
  9.  
  10. # Open function used with  filehandle and input file.
  11. open(MY_FILE, "$ARGV[0]") or die
  12.    "error: argument must be a file\n",
  13.    "usage: linenum.pl [filename]\n$!\n";
  14.  
  15. # Check if file is a directory
  16. if (!-f "$ARGV[0]"){
  17.    print "error: argument must be a file",
  18.    "\n",
  19.    "usage: linenum.pl [filename]\n";
  20.    exit 1;
  21. }
  22.  
  23. $COUNTER=1;  # Used for printing line numbers
  24.  
  25. # Loop and write lines from
  26. while($LINE <= <MY_FILE>){
  27.    # Adds leading zeros for numbers 1 digit long
  28.    if ($COUNTER<10){
  29.       print "000";
  30.    }
  31.    # Adds leading zeros for numbers 2 digits long
  32.    if (($COUNTER>9) && ($COUNTER<100)){
  33.       print "00";
  34.    }
  35.    # Adds leading zeros for numbers 3 digits long
  36.    if (($COUNTER>99) && ($COUNTER<1000)){
  37.    print "0";
  38.    }
  39.    # Prints line number and line
  40.    print "$COUNTER: $LINE";
  41.    $COUNTER+=1;
  42. }
  43.  
  44. exit 0;
# 2  
Old 10-10-2010
Please post your error message, thanks.

Also, have you tried invoking Perl with the -d switch? Then, your program will be run inside the Perl debugger.
# 3  
Old 10-10-2010
I tried to post my screen but i guess it was denied.

Here is the error and it just goes on and on I have to close down to end it.

Use of uninitialized value in numeric le (<=) at ./linenum.pl line 26, <MY_FILE> line 29

Use of uninitialized value in concatatenation (.) or string at ./linenum.pl line 40, <MY_FILE> line 29.
# 4  
Old 10-11-2010
Quote:
Originally Posted by zero3ree
...
perl code:
  1. #!/usr/bin/perl -w
  2. ...
  3. ...
  4. while($LINE <= <MY_FILE>){
  5. ...
  6. ...
(a) You haven't declared or assigned $LINE, and
(b) You are trying to compare, rather than assign the next line in the while loop.

That makes Perl go into an infinite loop.

The correct method is either this (fetch next line and test for truthfulness) -

Code:
...
while (<MY_FILE>) {
...

or this (fetch next line, assign to $LINE and test for truthfulness) -

Code:
...
while ($LINE = <MY_FILE>) {
...

tyler_durden
# 5  
Old 10-11-2010
One suggestion for future reference: adding

Code:
use strict;

to your scripts will save you a lot of debugging time in the future.

The error (actually warning) messages you posted are the result of your '-w' flag, and tell you that you should look at lines 26 and 40 for an uninitialized value.

What is that value? It's $LINE. $LINE is never initialized, so the while() loop will never stop.

Line 26 (the first line of the while() loop) doesn't do what you think it does. Rather than attempting to do a numeric comparison (<=), try initializing $LINE with a simple assignment:

Code:
while ( $LINE = <MY_FILE> ) {

This will set $LINE to each line of the file in turn, and then the rest of the loop code will execute with that value.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl loop until

I have a script that needs to wait on another script to finish. I created a sub routine to check the file for the number 0 but my until statement keeps on going. I tried eq, == and =~ but same thing. my $CHECKING_FILE = 1; do { sleep(5); $CHECKING_FILE = check_file(); ... (2 Replies)
Discussion started by: numele
2 Replies

2. Shell Programming and Scripting

perl while loop for each

I have the below scenario in perl cd $FIDE_RECEIVE ; # see the files that start with feedmgr.usfed.tips $CycleDate = &fi_get_curr_date('US','NIGHTLY_CYCLE','PROCESS'); head -1 GNM_GEO.DAT.EMBS* |grep -v GNM_GEO.DAT.EMBS | awk '{$4 " " $5}' output for above command :... (3 Replies)
Discussion started by: ptappeta
3 Replies

3. Shell Programming and Scripting

While Loop in Perl

Hi All I am reading the file using while loop in Perl someting like while (my $s=<F>){ chomp($s); .. .. .. } What i want to do is after the chomp statement i used some condition, if the condition is met then it should move forward otherwise it should read the new line. How Can it be... (4 Replies)
Discussion started by: parthmittal2007
4 Replies

4. Programming

while loop perl

I am trying to create a success and fail as below in a perl script : while echo$? is 2 it should append as below to .fail file ===================== if ( open(IN, "$outputfile")) { while( $my_line = <IN> ) { #print "$my_line \n" ; return 0; ... (3 Replies)
Discussion started by: sriram003
3 Replies

5. Shell Programming and Scripting

until loop Perl

I am trying to print out a section of a file begining at the start and printng until a character is found. My code and input file are below. This code is printing out every line except for the line with the character which is not what I want the out put should be a file with numbers 1-4. ... (3 Replies)
Discussion started by: cold_Que
3 Replies

6. Infrastructure Monitoring

Perl Loop Problem

Another newbie question... I can not figure out how to get this running using a loop. Here is what I have now. #!/usr/bin/perl use SNMP::Info; $list="list.list"; open(DAT, $list) || die("Can't Open List"); @raw_data=<DAT>; close(DAT); foreach $dest (@raw_data) {... (2 Replies)
Discussion started by: mrlayance
2 Replies

7. Shell Programming and Scripting

Perl - pass shell-vars into perl for input loop

I need to process a file line-by-line using some value from a shell variable Something like:perl -p -e 's/$shell_srch/$shell_replace/g' input.txt I can't make the '-s' work in the '-p' or '-n' input loop (or couldn't find a syntaxis.) I have searched and found... (4 Replies)
Discussion started by: alex_5161
4 Replies

8. Shell Programming and Scripting

perl loop keeps getting stuck

I am using a Perl script to open a series of files in a loop, separate the paragraph into lines, and output the lines into a new file. The code works perfectly fine, except when the source file is over a certain size the loop gets stuck and won’t move on to the next file. It still does what it is... (0 Replies)
Discussion started by: renthead720
0 Replies

9. Shell Programming and Scripting

for loop in perl

my $i; my $j; for($i=1;$i<=5;$i++) { for($j=$i;$j<5;$j++) { print " "; } print "$i\n"; } But the output i need is 1 12 123 1234 12345 Help me please (5 Replies)
Discussion started by: priyas
5 Replies

10. Shell Programming and Scripting

help with perl while loop

Can anyone tell me why this program won't kick out when the time gets beyond time in the loop? sub showtime { local($format,$military)=@_; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time); if ((! $military) &amp;&amp; ($hour &gt; 12)) {$hour-=12;} ... (2 Replies)
Discussion started by: methos
2 Replies
Login or Register to Ask a Question