CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
How to conditionally write 10 if statements at a time

 



Tejas
User

Oct 1, 2013, 2:47 AM

Post #1 of 11 (1300 views)
How to conditionally write 10 if statements at a time Can't Post

if (Check_Duplicate_Sale_Revenue($min_etid,$max_etid,$count,$avg_etid)) { next ; }
elsif (Check_PaymentMissing($min_etid,$max_etid,$count,$avg_etid)) { next ;}
elsif (Check_Refund_PaymentMissing($min_etid,$max_etid,$count,$avg_etid)) { next ;}
elsif (Check_Refund_Revunue_Missing($min_etid,$max_etid,$count,$avg_etid)) { next ;}
elsif (Check_Refund_Sale_Missing($min_etid,$max_etid,$count,$avg_etid)) { next ;}
elsif (Check_Sale_RevenueMissing($min_etid,$max_etid,$count,$avg_etid)) { next ;}
elsif (Check_Sale_Vs_Revenue_Mismatch($min_etid,$max_etid,$count,$avg_etid)) { next ;}
elsif (Refund_Payment_DoubleBooking($min_etid,$max_etid,$count,$avg_etid)) { next ;}


If the first if condition is false then next if condition has to be cleed ..
Is this the corect way?
Succeeding if condition has to be called if preceeding condition fails,
It has to go on till the if conition is true


BillKSmith
Veteran

Oct 1, 2013, 6:28 AM

Post #2 of 11 (1296 views)
Re: [Tejas] How to conditionally write 10 if statements at a time [In reply to] Can't Post

Your code is correct if it is inside a loop and you intend to go to the next iteration of the loop if any of these conditions is true. However, I find the following code to be clearer.

Code
use strict; 
use warnings;
my ($min_etid, $max_etid, $avg_etid);
COUNT:
for my $count ( 0 .. 10 ) {
my @etid = ($min_etid, $max_etid, $count, $avg_etid);
if (
Check_Duplicate_Sale_Revenue( @etid )
or Check_PaymentMissing( @etid )
or Check_Refund_PaymentMissing( @etid )
or Check_Refund_Revunue_Missing( @etid )
or Check_Refund_Sale_Missing( @etid )
or Check_Sale_RevenueMissing( @etid )
or Check_Sale_Vs_Revenue_Mismatch( @etid )
or Refund_Payment_DoubleBooking( @etid )
){
next COUNT;
}

# Do something
}


This code is equivalent to yours because of the 'short circuit' property of the 'or' operator. It stops checking as soon as it finds one that is true.

Factoring all the arguments into a single array emphasizes that the arguments to all the conditions are identical.
Good Luck,
Bill


Tejas
User

Oct 2, 2013, 1:56 AM

Post #3 of 11 (1281 views)
Re: [BillKSmith] How to conditionally write 10 if statements at a time [In reply to] Can't Post

Hi Bill

my ($min_etid, $max_etid, $avg_etid);
COUNT:
for my $count ( 0 .. 10 ) { //What is that we are counting here, I did not understannd


next COUNT;

As per my understnading , we running the loop just for ten times.
But i have around 2666666 lines in the input file and each line is a parameter

In Reply To

Code



      
    


BillKSmith
Veteran

Oct 2, 2013, 4:52 AM

Post #4 of 11 (1277 views)
Re: [Tejas] How to conditionally write 10 if statements at a time [In reply to] Can't Post

Sorry that I confused you. I meant to show how your fragment of code works in a loop. I do not know anything about your program so I made up a 'for' loop for the demo. "COUNT" is a statement label on that "for" statement. My "next" statement refers to that label. The label is not necessary, but it makes clear exactly what the "next" does.
Good Luck,
Bill


Tejas
User

Oct 2, 2013, 7:10 AM

Post #5 of 11 (1276 views)
Re: [BillKSmith] How to conditionally write 10 if statements at a time [In reply to] Can't Post

while ( $line = <MX_BR_Source>)
{
my($min_etid,$max_etid,$avg_etid,$count);
chomp;
my @row = split (",",$line);
#$min_etid = @_[4];
#$max_etid =@_[5];
#$avg_etid =@_[6];
#$count = @_[7];
if (
Check_Duplicate_Sale_Revenue( @row )
or Check_PaymentMissing( @row )
or Check_Refund_PaymentMissing( @row )
or Check_Refund_Revenue_Missing( @row )
or Check_Refund_Sale_Missing( @row )
or Check_Sale_RevenueMissing( @row )
or Check_Sale_Vs_Revenue_Mismatch( @row )
or Check_Refund_Payment_DoubleBooking( @row )
){
print "$COUNT \n";
next COUNT;
}
else
{
print Unknown_Bucket "$line";
}


And the sub routine

sub Check_Refund_PaymentMissing{

$min_etid = @_[4];
$max_etid =@_[5];
$avg_etid =@_[6];
$count = @_[7];
if(( $min_etid == 366 && $max_etid == 366 && $avg_etid == 366 && $count == 1 ) or ( $min_etid == 362 && $max_etid == 381 && $avg_etid == 369.666666666666666666666666666666666667 && $count == 3 ) )
{
print Refund_PaymentMissing "$line ";
return 1;
}
else {
return 0 ;
}
}

Above is the excat piece of code i am working on,
Im really curious to see how the while loop can be replaced as per you.
And also how do i print the COUNT , Can u please explain me the importace of COUNT here.

Also the wierd hardcoding of the numbers, tried using ceil and floor but doesnt work , so had to hard code.

Thanks


(This post was edited by Tejas on Oct 2, 2013, 7:49 AM)


BillKSmith
Veteran

Oct 2, 2013, 1:16 PM

Post #6 of 11 (1265 views)
Re: [Tejas] How to conditionally write 10 if statements at a time [In reply to] Can't Post

Your last question requires a rather long answer. In part, it is hidden in the FAQs and their references.

Code
perldoc -q "Why do I get long decimals"

The short summary is that is never a good idea to test for exact equality of floating point numbers. In you case use:

Code
&& abs( $avg_etid - 369.66667 ) < .00001

You may find that you need a similar fix for your other tests. They should be alright if they are always integers.


I did not intend you to replace the loop, only the if's. I did recommend adding a label to your loop. (I choose "COUNT" which probably was a bad choice.) Remove your "next COUNT" statement. You will do one print or the other then go to the next line automatically.


$count is still an issue. You are now getting its value from the eighth field of your input line. This is probably not what you mean. I do not know exactly what you want to count so I cannot show you how to do it.

Note: You must chomp $line (not the default $_).
Good Luck,
Bill


Tejas
User

Oct 2, 2013, 11:30 PM

Post #7 of 11 (1254 views)
Re: [BillKSmith] How to conditionally write 10 if statements at a time [In reply to] Can't Post

Thank You
I will do it as per your advice Bill
And i have already used chomp before sendin g the parameter .
So I dint chomp in the subroutine .
And its also working fine without chomp.

I have a lot to learn , but mere book learning and practiing is nt helping me complete these tasks, searching for the solution dymaically is help me :)

Kindly advice me the beast way of learning.
Thank You
Tejas

In Reply To


Laurent_R
Veteran / Moderator

Oct 3, 2013, 3:11 PM

Post #8 of 11 (1246 views)
Re: [Tejas] How to conditionally write 10 if statements at a time [In reply to] Can't Post

I definitely agree, reading tutorials, manuals, hand books, reference books and other materials is very important to increase knowledge coverage, but the most essential thing when learning a new programming language is to practice on real cases, where you really need to find a solution, because that 's where you get deeper knowledge, which is at least as important as a wide knowledge coverage. Nothing can replace real experience on real problems.

Having said that, don't get me wrong, a wide knowledge coverage is also essential, because it is what will tell you that "there is/must be/ might be/ ought to be/ " a better solution to your problem. And that is a key to progressing: you've read somewhere about a better way of doing things, maybe you don't remember the details, but you remember enough to test out new ways of doing things, that's where you might progressively become really knowledgeable, or perhaps even an expert.


Tejas
User

Oct 9, 2013, 11:31 PM

Post #9 of 11 (1194 views)
Re: [Tejas] How to conditionally write 10 if statements at a time [In reply to] Can't Post

7.05745E+11 -6.39 5-Dec-12 5-Dec-12 362 381
7.05761E+11 -1.93 5-Dec-12 5-Dec-12 362 381
7.05058E+11 0.02 3-Dec-12 3-Dec-12 362 362
7.05519E+11 12.99 4-Dec-12 4-Dec-12 362 362
7.0639E+11 -10.66 5-Dec-12 11-Dec-12 362 381
7.06391E+11 -20 5-Dec-12 11-Dec-12 362 381
7.0639E+11 -10.66 5-Dec-12 11-Dec-12 362 381

I have to add amounts for each month FROM THE OUTPUT FILE
EX:
DEC : 53.XX
NOV :54.XX


Laurent_R
Veteran / Moderator

Oct 10, 2013, 3:16 PM

Post #10 of 11 (1186 views)
Re: [Tejas] How to conditionally write 10 if statements at a time [In reply to] Can't Post

Sorry, that does not look difficult, but please tell us more on what you want to do, which amounts, etc.


BillKSmith
Veteran

Oct 10, 2013, 8:35 PM

Post #11 of 11 (1183 views)
Re: [Tejas] How to conditionally write 10 if statements at a time [In reply to] Can't Post

This looks familiar. Did you repeat it in a new thread? That seems like a good idea, but please tell us.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives