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

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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

In reply to Re: My "if" is failing! by 2teez
in thread My "if" is failing! by Sumtingwong

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (6)
    As of 2014-12-28 17:51 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (182 votes), past polls