Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    [usemodperl]: definitely

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (8)
    As of 2018-06-24 17:08 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (126 votes). Check out past polls.