Re: Variable division, assignment and sprintf in one line



On Nov 28, 2007, at 11:26 AM, Steve Bertrand wrote:
Is there a way that one can use sprintf on a variable and assign the
value back to the variable without having the leading $var= ?

I don't think you can do it directly, but you can use the aliasing properties of sub or for:

$a_really_long_variable_name = 100*1024*1024;
print "$a_really_long_variable_name\n";
divide($a_really_long_variable_name);
print "$a_really_long_variable_name\n";
sub divide { # ha ha I love this pun
$_[0] = sprintf ("%.2f", (($_[0] /=1024) /=1024));
}

or

$a_really_long_variable_name = 100*1024*1024;
print "$a_really_long_variable_name\n";
$_ = sprintf ("%.2f", (($_ /=1024) /=1024))
for $a_really_long_variable_name;
print "$a_really_long_variable_name\n";

Both give the same output:

104857600
100.00

The latter is most useful if you really are iterating over a list, of course.

--
Aaron Priven, aaron@priven,com, http://www.priven.com/aaron