In the script below, the variable $fline prints expected data before the if statement $x=-/go/ but not after (it seems that the 'if then' statement erases the $fline variable).

I can simply delete the if statement that contains $x=-/go/ then the $fline variable is then ok and unchanged and prints the expected data in both print statements. In fact, several different 'tests' (such as index) instead of the if statement cause the same problem.

What the heck is the problem with this screwy perl language???? Perl is driving me crazy!!! Why does putting in some type of 'if then' statement erase a variable???? Am I just an idiot???? Am I not seeing something obvious???? How do I stop it????

#!/usr/local/bin/perl -w print "Content-type:text/html\n\n"; #Content Header open(TOP, "first.htm"); # Open db for reading and display @farray=<TOP>; close (TOP); open(BOTTOM, "second.htm"); # Open db for reading and display @sarray=<BOTTOM>; close (BOTTOM); foreach $sline(@sarray){ chomp($sline); print "second $sline \n"; if (index ($sline, "pattern") > "-1") { foreach $fline(@farray){ chomp($fline); print "first $fline \n"; if ($fline =- /go/) {print "hello";} print "first $fline \n"; } } }

Edited by footpad, ~ Mon Aug 26 02:11:50 2002 (UTC) : Added <CODE> and other HTML formatting tags.


In reply to Variable Data Lost by perlidiot

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!
  • 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, details, 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, summary, 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.