Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Correct Use of Length Function

Status
Not open for further replies.

Geek8

Programmer
Apr 23, 2003
31
US
Hello,
I am trying to use the length function to determine the length of the input string and then print it if it is 1313 long. Else just skip it. This is what I have so far. I am not sure what I am doing wrong. Any help would be great.

#!/bin/ksh/perl -w
use strict;

##Open file for reading records##
my $file = '/u02/35bad.dat';
open (INPUTFILE, $file)
or die "could not open the $file $!\n";

##Open new outputfile##
my $out_file = '/u02/35_new_file';
open (OUTPUTFILE,">$out_file")
or die "could not open the $out_file $!\n";

my $line;
while ($line=<INPUTFILE>){
chomp($line);
while (length($line) == 1313){
print OUTPUTFILE $line;
}
}

close (INPUTFILE);
close (OUTPUTFILE);


Currently I am ending up a with a new file created, but it is 0 bytes. Can someone help me figure this out?

Thanks,

Geek.
 
Hi,

are you sure that in '/u02/35bad.dat' at least one line has ecxatly 1313 characters without the &quot;\n&quot;? If not, it's no wondering that nothing will be printed. Also, is this a text file or binary file? Does it run under Windows or Liunx?

Greetings

--
Smash your head on keyboard to continue...
 
Also, your program wouldn't terminate if there is a line with 1313 characters, look at this endless loop:

while (length($line) == 1313){
print OUTPUTFILE $line;
}

Greetings

--
Smash your head on keyboard to continue...
 
There are 8 records with that character length. The &quot;\n&quot; is the 1313th character. Is this the problem? The file is a Text file and also I am working on Linux, but have not tried it on Windows.

Geek.
 
Yes, thats the problem, because you chomped it off before you check it, therefore there were only 1312 characters left.

But still look out for the endless loop problem!

Greetings

--
Smash your head on keyboard to continue...
 
OH....yeah. That makes sense. Thanks for your help and direction. I really appreciate that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top