Re: [quickie] Rounding up to next multiple

[email protected] (Randal L. Schwartz)
Newsgroups gmane.comp.lang.perl.fun
Organization Stonehenge Consulting Services; Portland, Oregon, USA
Message-ID <[email protected]>
>>>>> "Paul" == Paul Makepeace <[email protected]> writes:

Paul> This isn't really perl, but it might be mildly fun for a few seconds:
Paul>   $len += $page_size - $len % $page_size if $len % $page_size;

Paul> Any way of doing this in a single arithmetic expression, without the if?

$len = ($len + ($page_size - 1)) % $page_size + 1;

I first learned this trick to convert 0..23 into bands of 1..12:

$hour12 = ($hour24 + 11) % 12 + 1;

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[email protected]> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.