in reply to My "if" is failing!
Hi Sumtingwong,
However, I put an "else" in there and the regex will no longer work in the opening "if" and defaults to the first "print" statement under the "else". When the "else" block is removed, the regex works
You are having this issue, because the else{...} goes with the first if(){...}i.e if (/^.*?<p>.*?( $sw )/) {...} and not if ($dec eq "y") {...} as intended.
How? You asked? Please see this from your code:
Meanwhile, what was intended was this:... while (<PLATTS>) { if (/^.*?<p>.*?( $sw )/) { # first if ... if ( $dec eq "y" ) { # second if ... } # second if CLOSED and DONE WITH } else { ## this was intended to work with second if statemen +t, but now works with the first if ... } ...
... while (<PLATTS>) { if (/^.*?<p>.*?( $sw )/) { # first if ... if ( $dec eq "y" ) { # second if ... } else { ## NOW else is attached to the second if, and now + works as intended ... } } ## END of first if statement. ...
That been said, there are a number of things, that could also help the implementation of your code, if you will.
-
Avoid bareword as file handles, use lexical variable in your open function.
Also use 3 argument open function. like so:open my $fh,'<',$filename or die "can't open file: $!"; -
All your "sandwiched" open functions, within the while loop and if statement can be stated clearly, and referred to using your lexical variable used as file handles like so:
open my $fh_output,'>>',$output or die "can't open file: $!"; open my $fh_input_file1,'<',$input_file1 or die "can't open file: +$!"; open my $fh_input_file2,'<',$input_file2 or die "can't open file: +$!"; ... if ( $dec eq "y" ) { print "\nadded \n\n"; ... print $fh_output "$sw \t $_ \n"; } ...
-
Update:
Please DO NOT parse an HTML file, using regex. Use a cpan module like HTML::Parser, HTML::TokeParser, HTML::TreeBuilder or any other one.
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
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: My "if" is failing!
by Sumtingwong (Novice) on Nov 29, 2012 at 03:16 UTC |
In Section
Seekers of Perl Wisdom