jdawes has asked for the wisdom of the Perl Monks concerning the following question:

I am using a script to look up information from a txt file,
each record on a separate line.
It works but i cant get an if statement to work.

I want basically

IF $es equals "$ 0.00 " then don't display a form.
But if it doesnt equal that then do display
a form the segment of the code looks like this:

if($es =~ /\b$ 0.00 \b/) {print "

which is contained about half way through the following routine..
can someone please show me how to write the proper if statement
sub searchcustomer { open (HANDLE, $datafilename) or &diedatafilenotfound; @data = <HANDLE>; close(HANDLE); $max = scalar (@data); $counter = 0; $found = 0; while ($counter <= $max) { $line = $data[$counter]; $jn = substr ($line, 0, 10); $ph = substr ($line, 10, 10); $ag = substr ($line, 20, 10); $na = substr ($line, 30, 10); $pr = substr ($line, 40, 10); $es = substr ($line, 50, 10); $ad = substr ($line, 60, 10); $st = substr ($line, 70, 65); $cm = substr ($line, 135, 65); $counter++; if (index($jn, $FORM{jobnumber}) != -1) {if (index($ph, $FORM{phnumber}) != -1) {print "<table border=1 bordercolor=888888 cellpadding=0 cellspacin +g=0><tr><td><table width=450> <tr><td width=0><b>Job Number: </b></td> <td width=100%>$j +n</td></tr> <tr><td width=0><b>Name: </b></td> <td width=100%>$n +a</td></tr> <tr><td width=0><b>Phone: </b></td> <td width=100%>$p +h</td></tr> <tr><td width=0><b>Agency: </b></td> <td width=100%>$a +g</td></tr> <tr><td width=0><b>Product: </b></td> <td width=100%>$p +r</td></tr> <tr><td width=0><b>Estimate: </b></td> <td width=100%>$e +s</td></tr> <tr><td width=0><b>Amount Due: </b></td> <td width=100%>$a +d</td></tr> <tr><td width=0><b>Status: </b></td> <td width=100%>$s +t</td></tr> <tr><td width=0><b>Comments: </b></td> <td width=100%>$c +m</td></tr> </table></td></tr> </table><br>\n"; $found = 1; } } } if($es =~ /\bone\b/) {print " <form action=/cgi-bin/FormMail.pl method=post name=accept_estimate_cos +t target=_self id=accept_estimate_cost> <input type=hidden name=job_number value=$jn /> <input type=hidden name=phone_number value=$ph /> <input type=hidden name=agency value=$ag /> <input type=hidden name=product value=$pr /> <input type=submit value='Accept Estimate' name=I_accept_the_Estimat +e> </form> <form action=/cgi-bin/FormMail.pl method=post name=accept_estimate_c +ost target=_self id=accept_estimate_cost> <input type=hidden name=job_number value=$jn /> <input type=hidden name=phone_number value=$ph /> <input type=hidden name=agency value=$ag /> <input type=hidden name=product value=$pr /> <input type=submit value='Decline Estimate' name=I_decline_the_Estim +ate> </form> </td></tr> <br>\n";} if ($found == 0) {print "Sorry, no match was found<br>\n";} print "<a href=$ENV{REQUEST_URI}>Click Here</a> to search again

Edit kudra, 2002-08-05 Added a readmore tag

Replies are listed 'Best First'.
Re: How to use IF
by fuzzyping (Chaplain) on Aug 05, 2002 at 02:15 UTC
    What part of it doesn't work? Are you getting error messages with the statement code, or are you just not matching? I don't see anything particuarly wrong with your statement, I expect that your regex isn't correct. Perhaps you've used spaces, rather than a tab, as your delimiter in /\b$    0.00 \b/ (or vice versa)? Take another look at your data to make sure you've got what you think you've got. One more thing... when you're trying to match a simple regex, it's a good idea to do some one-liner debugging, rather than continue to hack at hundreds (thousands) of lines of code. For example...
    perl -e 'open(DATA, "datafile.txt"); while (<DATA>) { print "$_\n" if +/\b$ 0.00 \b/; }'
    -fp
      I'm pretty confused, partly because I
      had a friend help me write most of this and now im just trying to
      add some bits to it. the following part of the
      script doesnt seem to do what i want it to

      if($es =~ /----------/) {print " <form action=/cgi-bin/FormMail.pl method=post name=accept_estimate_cos +t target=_self id=accept_estimate_cost> <input type=hidden name=job_number value=$jn /> <input type=hidden name=phone_number value=$ph /> <input type=hidden name=agency value=$ag /> <input type=hidden name=product value=$pr /> <input type=submit value='Accept Estimate' name=I_accept_the_Estimat +e> </form> <form action=/cgi-bin/FormMail.pl method=post name=accept_estimate_c +ost target=_self id=accept_estimate_cost> <input type=hidden name=job_number value=$jn /> <input type=hidden name=phone_number value=$ph /> <input type=hidden name=agency value=$ag /> <input type=hidden name=product value=$pr /> <input type=submit value='Decline Estimate' name=I_decline_the_Estim +ate> </form> </td></tr>

      regardless of whether "----------" shows up in the file
      that is read to memory it never shows the form buttons,
      i dont know why.

      I actually want the form to show up if $es is not equal
      to "----------" but im not sure how to do an
      IF NOT type of operation either.
      Sorry, Im really new to all this.

      In summary: two probs - my logical operation is the opposite of what i need,
      and it doesnt actually occur anyway.
Re: How to use IF
by sauoq (Abbot) on Aug 05, 2002 at 04:20 UTC
    a form the segment of the code looks like this:

    if($es =~ /\b$    0.00 \b/)

    You don't really need those \b's but you do need to escape your $ because it is a regular expression metacharacter meaning "Match the end of the line." That's probably not what you want.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: How to use IF
by DamnDirtyApe (Curate) on Aug 05, 2002 at 04:36 UTC

    Beyond this particular comparison, is the formatting of $es important? If not, I'd convert it to a plain numeric format, then do a numeric comparison.

    $str =~ tr/0-9.+-//cd ; # Not perfect, but probably good enough. if ( $str == 0 } { ... }

    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
Re: How to use IF
by smalhotra (Scribe) on Aug 05, 2002 at 04:15 UTC
    Try to use the perl debugger to see what is going on at the time of the given if(). Also do a print "<br>es='$es'\n"; to see what the value of $es is that you're trying to match the regex against.