Re: if/while/else problem
by kennethk (Abbot) on Mar 25, 2009 at 19:12 UTC
|
I'm thinking you are using -s wrong. The code if ( -s file ){ checks for the existence of a file associated with the filehandle file. You either need to provide the file name (a string) or the correct file handle (MYINPUTFILE). This would have been caught had you included use strict;use warnings.
In addition, you should really be using the three-argument form of open and testing to see if your open operations fail. Fixing the block structural issues mentioned above, perhaps you mean:
system "rm file2";
open(FILE2, ">", "file2") or die "Opening output failed: $!";
my $file = "file";
if ( -e $file and -s $file ){
open(MYINPUTFILE, "<", $file) or die "Opening input $file failed:
+$!";
while(<MYINPUTFILE>) {
my($line) = $_;
chomp($line);
print FILE2 "$line\n";
print FILE2 "you need to do this\n";
print FILE2 " \n";
}
} else {
print FILE2 "There is nothing to do\n";
}
| [reply] [d/l] [select] |
|
|
Unquoted string "file" may clash with future reserved word
Name "main::file" used only once: possible typo
-s on unopened filehandle file
| [reply] [d/l] [select] |
|
|
Thank you kennethk, this one did the trick. And thanks to all who gave suggestions, I was really stumped on this one.
| [reply] |
Re: if/while/else problem
by eff_i_g (Curate) on Mar 25, 2009 at 18:26 UTC
|
The else is being applied to your while statement, not your if. Try:
if ( -s file ){
while(<MYINPUTFILE>) {
my($line) = $_;
chomp($line);
print FILE2 "$line\n";
print FILE2 "you need to do this\n";
print FILE " \n";
}
}
else {
print FILE2 "There is nothing to do\n";
}
| [reply] [d/l] [select] |
|
|
Thanks for the suggestion, but it still prints my 'else' even when the 'if' should have been processed instead.
| [reply] |
|
|
Then there must be confusion with file. How about something like this?
use warnings;
use strict;
my $in = 'file'
my $out = 'file2';
open my $OUT, '>', $out or die $!;
if (-s $in) {
open my $IN, '<', $in or die $!;
while (<$IN>) {
my $line = $_;
chomp $line;
print $OUT "$line\n";
print $OUT "you need to do this\n";
print $OUT " \n";
}
}
else {
print $OUT "There is nothing to do\n";
}
| [reply] [d/l] [select] |
Re: if/while/else problem
by jwkrahn (Abbot) on Mar 25, 2009 at 19:26 UTC
|
open FILE2, '>', 'file2' or die "Cannot open 'file2' $!";
open MYINPUTFILE, '<', 'file' or die "Cannot open 'file' $!";
while ( my $line = <MYINPUTFILE> ) {
chomp $line;
print FILE2
"$line\n",
"you need to do this\n";
}
unless ( $. ) {
print FILE2 "There is nothing to do\n";
}
| [reply] [d/l] [select] |
Re: if/while/else problem
by locked_user sundialsvc4 (Abbot) on Mar 25, 2009 at 18:35 UTC
|
Exactly... a while statement cannot have an else clause.
Adding a single "}" at just the right place (and removing one that is now in the wrong place) should make Perl very happy again.
Bearing in mind that “you are among friends,” for we have all done it, you may now slap your forehead and repeat after me: “DOH!!” :-D
| |
|
|
DOH!! DOH!! :-) You were correct...however, even after making the change it did not fix my issue
| [reply] |
|
|
Your "issue" was that you were getting a syntax error. You now have a new issue because your syntax error is resolved. What you have now is a logic error. Unfortunately it's difficult for me to help you debug when all the code isn't here. However, something jumps out at me...
-s file - "file" is a bare word, are you using strict? You probably want -s "file" or -s $file or something.
| [reply] |
Re: if/while/else problem
by Bloodnok (Vicar) on Mar 25, 2009 at 18:27 UTC
|
Hmmm, the snippet you supply isn't anywhere near 54 lines long !!
However, that being said, the while loop appears to be terminated by the else clause, aka you're a } missing !
A user level that continues to overstate my experience :-))
| [reply] [d/l] [select] |
Re: if/while/else problem
by RoyCrowder (Monk) on Mar 25, 2009 at 18:28 UTC
|
Within your while loop you have a FILE error.
while(<MYINPUTFILE>) {
my($line) = $_;
chomp($line);
print FILE2 "$line\n";
print FILE2 "you need to do this\n";
print FILE " \n"; <------ *HERE*
} else {
Should probably be FILE2. I am making an assumption though because we don't have all of the code and you could possibly have FILE defined further up. | [reply] [d/l] |