Re: Variable division, assignment and sprintf in one line
- From: Jenda@xxxxxxxxxxx (Jenda Krynicky)
- Date: Thu, 29 Nov 2007 19:26:56 +0100
From: Steve Bertrand <iaccounts@xxxxxxxxxx>
$var = sprintf ("%.2f", (($var /=1024) /=1024)))
What the ...?!?
/= ?
It was an off the top of my head example of what I wanted to do. Without
being at the code at the time, I was simply thinking +=.
If you want to divide something use /. If you want to divide a
variable and store the result in a variable use /=. In either case /=
or += or any op= deep inside an expression is a red flag. Especially
if you do that twice to the same variable and in the end assign the
value of the statement to the very same variable. An error waiting to
happen.
What if you later decided that you actually want the formatted size
converted by megabytes (or megawhatever) in a different variable and
just changed the
$var = sprintf ("%.2f", (($var /=1024) /=1024)))
to
$formatted = sprintf ("%.2f", (($var /=1024) /=1024)))
? In that case the $var would still be modified. Simply don't use /=
if all you need is /.
It's similar to writing something like
$x = $x++ + ++$x;
Now what does that mean? Or what does
$x = ($x /= 48) + ($x *= 1.78);
mean?
You divide the value of $var by 1024, assign it back to $var, divide
it by another 1024, assign it again to $var, then format the
resulting number using sprinf() and assign it to $var?!? Why???
Why? Because I am not a professional, hence why I am asking at
'beginners' ;)
$var = sprintf ("%.2f", $var / (1024*1024));
And it will be more efficient because the multiplication will be done
at compile time (once) and you'll only do a single division each
time.
I did not know that multiplication would be done at compile time. Is
this true for any mathematical equation that does not include a runtime
variable?
Yes, that's about right. Not all matematical functions are evaluated
during the optimization, but the basic ones are. You can test it by
something like this:
perl -MO=Deparse -e "print sin(43)"
The -MO=deparse instructs Perl to compile the code into its internal
form, optimize it and spit it out as formatted Perl code again.
Which is also more precise, though in this case it doesn't seem
to matter.
'twasn't about precision. It was a theoretical 'how can I do this?'.
Sure. That was just an unimportant remark in case you sometime later
needed to care about precision :-)
Oh, and furthermore, if you happened to read my original post, I had
asked how to perform what you state above, WITHOUT needing the left hand
side.
I happened to read not just your message but also the responses so I
knew that question has already been answered. You received two good
suggestions, to use a subroutine and to use for to alias the long
$var to $_ so there was nothing to add to that.
Jenda
===== Jenda@xxxxxxxxxxx === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
.
- References:
- Variable division, assignment and sprintf in one line
- From: Steve Bertrand
- Re: Variable division, assignment and sprintf in one line
- From: Jenda Krynicky
- Re: Variable division, assignment and sprintf in one line
- From: Steve Bertrand
- Variable division, assignment and sprintf in one line
- Prev by Date: Re: "premature end of header" script error
- Next by Date: Re: Usage: DCE::RPC::new (new DCE::RPC, $auth) at
- Previous by thread: Re: Variable division, assignment and sprintf in one line
- Next by thread: Re: Variable division, assignment and sprintf in one line
- Index(es):
Relevant Pages
|