Re: Variable division, assignment and sprintf in one line
- From: aaron@xxxxxxxxxx (Aaron Priven)
- Date: Wed, 28 Nov 2007 11:49:02 -0800
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
- References:
- Variable division, assignment and sprintf in one line
- From: Steve Bertrand
- Variable division, assignment and sprintf in one line
- Prev by Date: Re: fixed list combinatorics
- Next by Date: Re: Variable division, assignment and sprintf in one line
- Previous by thread: Variable division, assignment and sprintf in one line
- Next by thread: Re: Variable division, assignment and sprintf in one line
- Index(es):