Hi bhargavkanakiya,
from your code:
foreach my $filename( @files ){
my $file = "text_000".$variable."."."lab";
my $outfile = "$outdirname/"."$file";
open my $output , ">$outfile" || die "Can't open the output file!"
+;
print "file: $file\n";
while ( my $line = <$filename> ) {
$line =~ s/^\s+//;
$line =~ s/ +/ /g;
$line =~ s/\s+$/\n/g;
print $output $line;
}
close ($output);
$variable++;
}
$filename is the name of the file to open, and read from. In your code, "$filename" is
not a FILEHANDLE.
In Perl Programming, you need a file-handle in the
open function like thus:
open FILEHANDLE,MODE,EXPR Note: that you are not
opening, your file named "$filename" to read from. You are just using it in the while loop with
readline function as
while ( my $line = <$filename> ) {...} which should really be
while ( my $line = <FILEHANDLE> ) {...}
So, your code should be something like:
foreach my $filename( @files ){
my $file = "text_000".$variable."."."lab";
my $outfile = "$outdirname/"."$file";
open my $output,'>',$outfile or die "Can't open the output file: $
+!"; ## updated
open my $fh,'<',$filename or die "can't open file: $!"; ## add
+ed
print "file: $file\n";
while ( my $line = <$fh> ) {
$line =~ s/^\s+//;
$line =~ s/ +/ /g;
$line =~ s/\s+$/\n/g;
print $output $line;
}
close $fh or die "can't close file: $!"; ## added
close $output or die "can't close file: $!"; ## updated
$variable++;
}
NOTE: All codes are not tested please.
If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author
unknown to me
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.