in reply to How would you rewrite this?

I was uncertain whether the 5th call should have had  objects[4] -- in fact, maybe there are "copy/paste" problems in both the 4th and 5th calls.

If the calls were supposed to follow a regular pattern (as opposed to the last two calls breaking the pattern), then this would be how I would phrase it:

if ( $result_ref{'agency_id'} != 0 ) { my $i = 0; for my $k (qw/agency_id advertiser campaign_id admin_contact_id te +ch_contact_id) { get_updates($objects[$i++], $k, $result_ref->{$k}) if (defined $result_ref->{$k}); } }
On the other hand, if the last two calls are supposed to be exactly as in the OP (and do not follow a regular a pattern), then make an extra array or hash to provide the necessary values for the index into @objects and for the second arg to get_updates().

Replies are listed 'Best First'.
Re^2: How would you rewrite this?
by ihb (Deacon) on Apr 27, 2005 at 01:37 UTC

    Note that your code doesn't follow the original with regard to the index. You only increase the index if the if branch is evaluated. You need to increase $i outside the if. I'd probably rewrite it to this though:

    my @fields = qw/ ... /; for (0 .. $#fields) { get_updates($objects[$_], $fields[$_], $result_ref->{$fields[$_]} if defined $result_ref->{$fields[$_]}; }
    A structure like that doesn't invite to get things out of sync.

    ihb

    See perltoc if you don't know which perldoc to read!

Re^2: How would you rewrite this?
by jacques (Priest) on Apr 27, 2005 at 01:34 UTC
    The array indexes are correct. That is not a typo, although I can see how it could look like one. There is only one major typo -- and that was agency_id key in every if statement.