Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PERL if else 1

Status
Not open for further replies.

attn2risky

IS-IT--Management
Jul 24, 2008
4
US
Hi all, I've just been introduced to PERL and am looking for advice on the following snippet of code. I want to have the $TotalDemandData be a result of the input variables it already is (this works) and the new BOH data (which I named $BOHSub) subtract the Total Demand and then ONLY print the result if it's above zero.

#!/opt/perl/bin/perl -w

system "rm -f ./new_forecastsales.out";

open (NEW, ">>new_forecastsales.out");
open (MATRIX, "matrix.out");
#<MATRIX>;

while (<MATRIX>) {
@fields = split (/,/);
$Enterprise = $fields[0];
$Site = $fields[1];
$Item = $fields[2];
$ForecastCount = $fields[3];
$ForecastAvg = $fields[4];
$SalesCount = $fields[5];
$SalesAvg = $fields[6];
$BOH = $fields[7];
$TotalDemandData = "$ForecastCount" * "$ForecastAvg" + "$SalesCount" + "$SalesAvg";
$BOHSub = "$BOH" - "$TotalDemandData";
if ($BOHSub > 0)
{
print NEW $BOHSub;
}
else
{
print NULL; #this is obviously wrong
}

print NEW $Enterprise, ",", $Site, ",", $Item, ",", $BOHSub, "\n";
}

close (MATRIX);
close (NEW);

my main issue is, how do i get the "else" statment to print nothing(or simply...leave that line out in the final print statement)?

i'm very new so i tried to put in as much detail about my problem as possible. thanks so much!!
 
just take it out of the code. YOu have other issues though, quoting scalars in perl code is a bad habit to get into:

Code:
$TotalDemandData = "$ForecastCount" * "$ForecastAvg" + "$SalesCount" + "$SalesAvg";
        $BOHSub = "$BOH" - "$TotalDemandData";

the above is better written as:

Code:
$TotalDemandData = ($ForecastCount * $ForecastAvg) + $SalesCount + $SalesAvg;
        $BOHSub = $BOH - $TotalDemandData;

quoting scalars needlessly can create very hard to find bugs in your perl code, not to mention it just introduces inefficiency. Only use quotes when making a new string out of a scalar. It might also be applicable to forcing numbers to be evaluated as strings in certain situations.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks so much!!

I tried both of the recs above, both don't seem to give me what I'm trying to pull out of the input. I'm getting negative numbers of the BOH which shouldn't appear on the output. It almost seems as if it's completely ignoring any if statment I put in before or in the print command.

I also realized an error of my own, I don't want it to print *in* the if statment, the only thing i want to print is the multiple variables and IF the BOH is above zero...


is there any way to void out any lines that have a BOHSub below 0?
 
this is what i have now:

#!/opt/perl/bin/perl -w

system "rm -f ./new_forecastsales.out";

open (NEW, ">>new_forecastsales.out");
open (MATRIX, "matrix.out");
#<MATRIX>;

while (<MATRIX>) {
@fields = split (/,/);
$Enterprise = $fields[0];
$Site = $fields[1];
$Item = $fields[2];
$ForecastCount = $fields[3];
$ForecastAvg = $fields[4];
$SalesCount = $fields[5];
$SalesAvg = $fields[6];
$BOH = $fields[7];
$TotalDemandData = ($ForecastCount * $ForecastAvg) + $SalesCount + $SalesAvg;
$BOHSub = $BOH - $TotalDemandData;

print NEW if ($BOHSub > 0), $Enterprise, ",", $Site, ",", $Item, ",", $BOH, "\n";
}

close (MATRIX);
close (NEW);

it's doing the right thing in that it's printing only the BOHSub if it's above zero, but not the corresponding line ONLY if the BOHSub is above zero. any suggestions?
 
Code:
if ($BOHSub > 0){
 $BOHSub = $BOH - $TotalDemandData;
 print NEW "$Enterprise,$Site,$Item,$BOH\n";
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
I'm surprised this is not generating any warnings or errors:

Code:
print NEW if ($BOHSub > 0), $Enterprise, ",", $Site, ",", $Item, ",", $BOH, "\n";

I guess in that strange context the "if (condition)" is just being ignored. travs69 posted the correct syntax you should use.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I think you where trying for:
Code:
print NEW $Enterprise, ",", $Site, ",", $Item, ",", $BOH, "/n" if ($BOHSub > 0);
The if statement always goes on the end when using an inline statement like that.
 
This is an example where you can quote the scalars because you are making a new string out of multiple scalars, plus it makes the code easier to read:


Code:
print NEW "$Enterprise,$Site,$Item,$BOH\n" if ($BOHSub > 0);

Side note: /n should be \n.




------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks everyone so much! Kevin your suggestion worked perfectly, thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top