473,378 Members | 1,419 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Fixing an error from using Loop to read a file

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:

Expand|Select|Wrap|Line Numbers
  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;
  45.  
  46.  
This is the error and I can't figure out what it means.

"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."

This error doesn't stop either I have to close my program to end it.
Oct 11 '10 #1
1 1942
RonB
589 Expert Mod 512MB
Your script has a number of problems, but lets start with this:

change line 26 ti this:
Expand|Select|Wrap|Line Numbers
  1. while( my $LINE = <MY_FILE> ){
Oct 11 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Brian Beck | last post by:
Hi. I'm having some problems with code based directly on the following httplib documentation code: http://www.zvon.org/other/python/doc21/lib/httplib-examples.html I've included the code and...
0
by: MrTulley | last post by:
UDB V8 FP4 AIX 5.1 I am receiving the following error message while using the autoloader to load a pipe delimited file into a partitioned table: SQL3004N The filetype parameter is not valid....
40
by: Abby | last post by:
My .dat file will contain information like below. /////////// First 0x04 0x05 0x06 Second 0x07
1
by: HNguyen | last post by:
Access denied error while uploading a file to a web server. I am getting the following error while trying to upload a file to our web server: Error: Access to the path...
0
by: Ahmed A. | last post by:
This will be very helpfull for many! Using RichTextBox Read/Write Unicode File http://www.microsoft.com/indonesia/msdn/wnf_RichTextBox.as p Private Function ReadFile(ByVal myfile As String)...
8
by: Pedro Pinto | last post by:
When compiling my program i got this error: Error: 'for' loop initial declaration used outside c99 mode What is it and how can i solve it? Thanks in advance! Regards
2
by: somequestion | last post by:
During copying file , wanna read file Size like this string CheckFileSize(string fileName) { if( fileName == null ) return; FileInfo fi = new FileInfo(fileName); return fi.Length.ToString();...
0
by: anibakore | last post by:
Hi All, I am using product.xml for vs2005 pre-requisites. I am using <RegistryCheckinside <InstallCheck>. I am trying to read one registry value of installed software. I can see it through...
2
by: contractsup | last post by:
Environment: $ uname -a AIX <withheld2 5 000100614C00 $ db2level DB21085I Instance "<withheld>" uses "32" bits and DB2 code release "SQL08024" with level identifier "03050106"....
1
by: prads | last post by:
Hello, the foll pgm displays some wierd looking characters at the output when cout<<buffer<< is done. Pls tell me why it happens and also correct the error. Thanks, Prads int main () { ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.