Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: My "if" is failing!

by 2teez (Priest)
on Nov 28, 2012 at 22:57 UTC ( #1006124=note: print w/replies, xml ) Need Help??

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:

... 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 ... } ...
Meanwhile, what was intended was this:
... 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

Replies are listed 'Best First'.
Re^2: My "if" is failing!
by Sumtingwong (Novice) on Nov 29, 2012 at 03:16 UTC

    Ok, I got it now. This helps a lot, thanks!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1006124]
[LanX]: well even my random fingers have memory ...
[Eily]: you could have tried randomly swapping your fingers
[Eily]: enigma style
[choroba]: so it was you who typed the whole phrases?
[Yaerox]: I feel like i'm not seeing the forest for the trees. Can someone give me a hint how to research converting all kinds of files into ansi? If origin file is utf8 convert to ansi, is origin file is ansi then skip file.

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (13)
As of 2017-03-28 13:04 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (331 votes). Check out past polls.