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:
Problem with IF statement

 



Fishhead
Novice

Aug 4, 2009, 7:32 AM

Post #1 of 14 (1021 views)
Problem with IF statement Can't Post

I am beginning to get the hang of reading perl code, but have a long way to go. My current problem is below. I am modifying downloaded code that works fine. I wish to change the program to charge "member" a different rate than non-members. I will worry about accessing records that indicate member/non-member later, so for now I am working on the IF member portion of the code change. When I run the program this section of code always uses the values in the ELSE option. I can drop the IF member and code it for either set of CLOSEDPERCENTFILES and that works, but when I add the IF member = code it does not seem to work. Any thoughts?



my ($percent, $percentfee, $percentcalcamount, $member, @allclosedrates);

$member eq "yes";

if ($closedamount != 0) {
if (!$percent) {

if ($member eq "yes") {
open (CLOSEDPERCENTS2FILE, $config{'closepercent2file'});
@allclosedrates = <CLOSEDPERCENTS2FILE>;
close(CLOSEDPERCENTS2FILE);
}

else {
open (CLOSEDPERCENTSFILE, $config{'closepercentfile'});
@allclosedrates = <CLOSEDPERCENTSFILE>;
close(CLOSEDPERCENTSFILE);
}

... does other stuff }}


1arryb
User

Aug 4, 2009, 8:12 AM

Post #2 of 14 (1019 views)
Re: [Fishhead] Problem with IF statement [In reply to] Can't Post

Hi Fishhead,

Your code snippet is incomplete and contains code that will never execute. See my comments below.


Code
my ($percent, $percentfee, $percentcalcamount, $member, @allclosedrates); 

$member eq "yes";

if ($closedamount != 0) { # This will never execute because $closedamount isn't set.
if (!$percent) {
if ($member eq "yes") {
open (CLOSEDPERCENTS2FILE, $config{'closepercent2file'});
@allclosedrates = <CLOSEDPERCENTS2FILE>;
close(CLOSEDPERCENTS2FILE);
} else {
open (CLOSEDPERCENTSFILE, $config{'closepercentfile'});
@allclosedrates = <CLOSEDPERCENTSFILE>;
close(CLOSEDPERCENTSFILE);
}
} # ENDIF !$percent
} # ENDIF $closedamount


Cheers,

Larry


FishMonger
Veteran / Moderator

Aug 4, 2009, 8:51 AM

Post #3 of 14 (1015 views)
Re: [Fishhead] Problem with IF statement [In reply to] Can't Post

In addition to Larry's comment, I'd change the if/else block to this:

Code
my $file = $member eq "yes" ? $config{'closepercent2file'} : $config{'closepercentfile'}; 

open my $closed_percent_file, '<', $file or die "failed to open '$file' $!";
@allclosedrates = <$closed_percent_file>;



Fishhead
Novice

Aug 4, 2009, 8:59 AM

Post #4 of 14 (1014 views)
Re: [1arryb] Problem with IF statement [In reply to] Can't Post

Thanks Larry

Perhaps I was not clear. The code I included was extracted out of a sub routine that did work before began making my changes. I did not include the code above or below my snippet. Closeamount was set earlier.


FishMonger
Veteran / Moderator

Aug 4, 2009, 9:02 AM

Post #5 of 14 (1012 views)
Re: [Fishhead] Problem with IF statement [In reply to] Can't Post

This line is not doing what you think.

Code
$member eq "yes";

You probably meant it to be:

Code
$member = "yes";



1arryb
User

Aug 4, 2009, 9:04 AM

Post #6 of 14 (1010 views)
Re: [FishMonger] Problem with IF statement [In reply to] Can't Post

D'oh! Missed that one.


Fishhead
Novice

Aug 4, 2009, 9:44 AM

Post #7 of 14 (1004 views)
Re: [FishMonger] Problem with IF statement [In reply to] Can't Post

I changed the eq to = and the "yes" block was selected. However, when I changed $member = "yes" to $member = "no", the yes block was still being selected. I am getting very confused. Any help?


KevinR
Veteran


Aug 4, 2009, 10:12 AM

Post #8 of 14 (1002 views)
Re: [Fishhead] Problem with IF statement [In reply to] Can't Post


In Reply To
I changed the eq to = and the "yes" block was selected. However, when I changed $member = "yes" to $member = "no", the yes block was still being selected. I am getting very confused. Any help?


Post your latest code, there is no way to tell unless we see the code.
-------------------------------------------------


Fishhead
Novice

Aug 4, 2009, 10:43 AM

Post #9 of 14 (998 views)
Re: [KevinR] Problem with IF statement [In reply to] Can't Post

KevinR and others-



Below is the code for the entire subroutine:



sub transaction_percentage_closed {

my $user = $_[0];
my $trans = $_[1];
my $firstbid = $_[2];
my $lastbid = $_[3];
my $reserve = $_[4];

my ($closedamount, $closednotes, $closedwhatamount);

if (($reserve > $lastbid) && ($reserve > 0)) {
$closedamount = 0;
$closednotes = "Reserve of $config{'currencytype'}$reserve not met, no charges.";
$closedwhatamount = "reserve price";
}

elsif (($reserve <= $lastbid) && ($reserve > 0)) {
$closedamount = $lastbid;
$closednotes = "Reserve of $config{'currencytype'}$reserve met, Closing percentage fee based off final bid of $config{'currencytype'}$lastbid.";
$closedwhatamount = "final price";
}

elsif ($lastbid >= $firstbid) {
$closedamount = $lastbid;
$closednotes = $closedamount;
$closedwhatamount = "final bid";
}

else {
$closedamount = 0;
$closednotes = "No bids received on item, no charges.";
$closedwhatamount = "starting bid";
}

$closedamount = &parseprice($closedamount);

my ($percent, $percentfee, $percentcalcamount, $member, @allclosedrates);

$member = "yes"; ### WHEN I CHANGE THIS VALUE TO NO, THE YES BLOCK IS STILL BEING SELECTED ###

if ($closedamount != 0) {
if (!$percent) {

if ($member = "yes") {
open (CLOSEDPERCENTS2FILE, $config{'closepercent2file'});
@allclosedrates = <CLOSEDPERCENTS2FILE>;
close(CLOSEDPERCENTS2FILE);
}

else {
open (CLOSEDPERCENTSFILE, $config{'closepercentfile'});
@allclosedrates = <CLOSEDPERCENTSFILE>;
close(CLOSEDPERCENTSFILE);
}

my $fielddelimiter = ":::";

my ($closedrangefee, $closedrangelow, $closedrangehigh);
my $closedraterecord;
foreach $closedraterecord (@allclosedrates) {
($closedrangefee, $closedrangelow, $closedrangehigh) = split /$fielddelimiter/, $closedraterecord;

my $showclosedrangefee = $closedrangefee * 100;

if (($closedamount >= $closedrangelow) && ($closedrangehigh >= $closedamount)) {$percentcalcamount = &parseprice(-($closedamount * $closedrangefee));}

}
}

else {
$percentfee = &parseprice($percentfee);
$closednotes = "Closing fee of $config{'currencytype'}$percentfee charged.";
$percentcalcamount = $percentfee;
}
}
else {
$percentcalcamount = &parseprice($closedamount);
}



&apply_transaction($user, $trans, $percentcalcamount, $closednotes);

}


FishMonger
Veteran / Moderator

Aug 4, 2009, 11:24 AM

Post #10 of 14 (996 views)
Re: [Fishhead] Problem with IF statement [In reply to] Can't Post

Please use the code tags when posting your code!


Code
if ($member = "yes") {

That is doing a numerical comparison.

When assigning the var, you need to use '=' but when doing a string comparison, you need to use 'eq'.

Change that line to:

Code
if ($member eq "yes") {



(This post was edited by FishMonger on Aug 4, 2009, 11:24 AM)


KevinR
Veteran


Aug 4, 2009, 11:38 AM

Post #11 of 14 (990 views)
Re: [FishMonger] Problem with IF statement [In reply to] Can't Post


In Reply To
Please use the code tags when posting your code!


Code
if ($member = "yes") {

That is doing a numerical comparison.

When assigning the var, you need to use '=' but when doing a string comparison, you need to use 'eq'.

Change that line to:

Code
if ($member eq "yes") {



Actually its the assignment operator, so he is checking if the assignment of "yes" to $member is true, which of course it always is.
-------------------------------------------------


(This post was edited by KevinR on Aug 4, 2009, 11:40 AM)


FishMonger
Veteran / Moderator

Aug 4, 2009, 11:43 AM

Post #12 of 14 (985 views)
Re: [KevinR] Problem with IF statement [In reply to] Can't Post

Kevin,

You didn't need to edit out that comment. I thought is was funny, and for the most part true. :)


Fishhead
Novice

Aug 4, 2009, 11:52 AM

Post #13 of 14 (983 views)
Re: [KevinR] Problem with IF statement [In reply to] Can't Post

Thank all of you for your help. I did not pick-up on the subtly of the difference between eq and = when reading the O'Riely manual. It now works fine. You may hear back from me once I begin to tackle the process of accessing the registration files for membership status.


KevinR
Veteran


Aug 4, 2009, 3:05 PM

Post #14 of 14 (979 views)
Re: [FishMonger] Problem with IF statement [In reply to] Can't Post


In Reply To
Kevin,

You didn't need to edit out that comment. I thought is was funny, and for the most part true. :)


I'm relieved to know you thought it was funny. After I posted it and reread it I thought it might be taken the wrong way by others.

PS... I hope the dementia's not too bad..... yet Crazy
-------------------------------------------------

 
 


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

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