Paul Johnson wrote:
> On Thu, Oct 12, 2006 at 10:18:31PM +1300, Robin Sheat wrote:
> 
>>I have a section of a program that is this:
>>
>>        my $users = get_all_users();
>>        foreach my $user (@$users) {
>>                my $details = get_user_details($user);
>>                my $sum=0;
>>                my $count=0;
>>                for (my $i=1; $i<@$details; $i+=2) {
>>                        $sum += @$details[$i];
>>                        $count++;
>>                }
>>                $means{$user} = $sum/$count;
>>        }
> 
> I'd probably try replacing the middle of your loop with something like:
> 
>   my ($sum, $count, $i) = (0, @$details / 2, 1);
>   $sum += @$_ for grep $i ^= 1, @$details;
            ^^^
That should be:

   $sum += $_ for grep $i ^= 1, @$details;


John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.       -- Larry Wall

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to