That was my initial thought but that would make 30 => 2 and not 1. So I think he wants int( ($num-1) / 30 ) + 1.
#!/usr/bin/perl
use strict;
use warnings;
my @nums = ( 1 .. 1000 );
foreach my $num ( @nums ) {
my $test_n = int( ($num-1) / 30 ) + 1;
print "Num: $num - $test_n \n";
}
| [reply] [d/l] |
Um, technically this won't work either for $total_nums = 1, much as friedo's version doesn't work for $total_nums values of 30, 60, 90, etc., based on the OP's original code. Looks like diotalevi's example using ceil() is better.
Update: Ah, I see you updated your version to add 1 after getting the int() value. Fine.
(Blush)Another Update: Uh, actually, not fine. Now yours doesn't work for values of 31, 61, etc.
| [reply] |
$ perl -e 'print int( (31-1) / 30 ) + 1, "\n"'
2
$ perl -e 'print int( (61-1) / 30 ) + 1, "\n"'
3
| [reply] [d/l] |