Gossamer Forum
Home : General : Perl Programming :

if (@value[1] > 50)

Quote Reply
if (@value[1] > 50)
Hello. Can you have a look? This should increase $lit (+1) when cents are higher than .50. The problem is: It is incresing in any case (even if the value is lower than 50)
Thanks very much for this service.
Robert
-------------------

if ($litval = 1){
$lit = $iltot*1936.27; #multiply currency
@cent = split/\./,$lit; #get cents
$lit=~s/.@cent[1]//; #remove cents

if (@cent[1] > 50){$lit++;} #round

}

else{
$iltot=~s/\.//;


}
Quote Reply
Re: if (@value[1] > 50) In reply to
Code:
$lit = $iltot * 1936.27;
($lit,$cent) = split(/\./, $lit);
$lit++ if ($cent > 50);

-- Gordon --


------------------
$blah='82:84:70:77';
print chr($_) foreach (split/:/,$blah);
Quote Reply
Re: if (@value[1] > 50) In reply to
Thank you Gordon.
Unfortunately I have the same problem. "if" is not considered and the value $lit is always incremented.
Quote Reply
Re: if (@value[1] > 50) In reply to
That's because your boolean comparison isn't boolean.
try
if ($litval == 1) {

or
if ($litval eq "1") {

------------------
$blah='82:84:70:77';
print chr($_) foreach (split/:/,$blah);
Quote Reply
Re: if (@value[1] > 50) In reply to
To extend on what Gordon is saying,

"=" is an assignment operator
"==" or "eq" is for comparison

--mark

------------------
Due to repeated requests for support via ICQ, I am once again offering support online.

The ONLY number I will accept requests on is #53788453. Messages sent to my other numbers will not pass through to me, as I have security settings set to ignore messages from users not on that list.

I don't know how often I will be active on this number, as it is for only when I am free to offer assistance.

Could this signature be any longer? :)
Quote Reply
Re: if (@value[1] > 50) In reply to
Ok.
but $litval is working. In fact all other operations are executed! What I mean is .... How can I tell this script to work ONLY if the value is higher than 50 .... if ($value > 50) ?? Correct?
Quote Reply
Re: if (@value[1] > 50) In reply to
What value? There is no $value in your code.

--mark

------------------
Due to repeated requests for support via ICQ, I am once again offering support online.

The ONLY number I will accept requests on is #53788453. Messages sent to my other numbers will not pass through to me, as I have security settings set to ignore messages from users not on that list.

I don't know how often I will be active on this number, as it is for only when I am free to offer assistance.

Could this signature be any longer? :)
Quote Reply
Re: if (@value[1] > 50) In reply to
of course $litval works... in fact, it'll work too much:

if($litval = 1) { STATEMENTS } is like saying:

$litval = 1;
if($litval) { STATEMENTS }

thus, $litval will always be 1 (which means TRUE to perl), and the STATEMENTS will always be executed.

also, when you use an element of an array (@list, if you will), you use $foo[bar] =...
i'm not sure whether or not the split function worked or not, but for clarity's sake (get into this habit or you'll shoot u'rself in the foot later on), put parens around the args of any function so instead of :
split/\./,$lit;
use: split(/\./, $lit); #<---notice space between comma and arg is easier on the eyes, also


what you should use is:

Code:
if ($litval == 1){
$lit = $iltot * 1936.27; #multiply currency
@cent = split(/\./,$lit); #get cents
$lit=$cent[0]; #remove cents

if ($cent[1] > 50){$lit++;} #round

}



--Tom
Quote Reply
Re: if (@value[1] > 50) In reply to
Hi!<br>
The statement '$litval' is correct.... I mean it is working! What I can't solve is this:<br>
if (@cent[1] > 50){$lit++;}
<br>
This is always executed Frown even if the value is below 50. <br>
example: '10.25' is converted into '11'
Quote Reply
Re: if (@value[1] > 50) In reply to
To complete my previous message: I already tried to remove " if ($litval = xx){} ". It works but $lit++
Quote Reply
Re: if (@value[1] > 50) In reply to
Why waste 2 lines when you can do it in one?
And why use braces in a single line if statement?
Also, the reason your code is incrementing every time is because the $cent var has something 4 or 5 or 10 digits long. Try this:
Code:
if ($litval == 1) {
$lit = $iltot * 1936.27;
($lit,$cent) = split(/\./, $lit);
$cent = substr($cent, 0, 2);
$lit++ if ($cent > 50);
}

-- Gordon --


------------------
$blah='82:84:70:77';
print chr($_) foreach (split/:/,$blah);

Quote Reply
Re: if (@value[1] > 50) In reply to
Great it works. I don't understand why but it works.
substr? does it mean something like "from 0 to 2 digits"?
Thanks very much.
Quote Reply
Re: if (@value[1] > 50) In reply to
'substr' takes a sub-string of a given string. check out perldoc -f substr as well as perldoc perlfunc.
Code:
$cent = substr($cent, 0, 2);

just sets $cent to the first 2 digits of $cent. That way you aren't comparing 34263 > 50, you are comparing 34 > 50.
-- Gordon --


------------------
$blah='82:84:70:77';
print chr($_) foreach (split/:/,$blah);