[Omaha.pm] $ids[$i++]

Jay Hannah jay at jays.net
Fri Oct 19 13:04:49 PDT 2007


Andy Lester wrote:
>> But now my C++ mid-term I just took last night has me thinking like this:
>>
>> my $i = 0;
>> foreach my $single_reason (split /\|/, $reason) {
>>    my $id = $ids[$i++];
>>     
>
> Why do you want to know where in the array you are?  Should you be
> avoiding the loop index entirely?

Here is more context. (80% of the real but irrelevant code has been removed)


# We need the list of all IDs first so we can set related_uniform_ids
# to all IDs on each ticket.
my @ids = (get_id($dbh));
foreach (split /\|/, $reason) {
    push @ids, ($ids[-1] + 1);
}
pop @ids;
my $i = 0;
foreach my $single_reason (split /\|/, $reason) {
    my $id = $ids[$i++];
    my $uniform = Model::MySQL::Simple::uniform->new(Globals=>$Globals);
    $uniform->set_id($id);
    $uniform->set_related_uniform_ids(join "|", @ids);
    $uniform->commit(filter=>"id = '$id'");
}
my $url = $q->url();
$url.="?dowhat=success&id=" . (join "|", @ids);
print $q->redirect("$url");



I'm creating a set of entries in the "uniform" table of our database. I 
don't know how many I'm creating -- that depends on how many reasons 
there are in the pipe-delimited list stored in $reason.

Each row in the table needs a unique ID, but I also want a pipe 
delimited list of all of the IDs I'm creating set into the 
related_uniform_ids attribute of every row.

So, first I create @ids. Then I loop again. $i grabs the individual ID 
for each ticket.

I 'spose I could re-write it to be more readable, but it's probably not 
bad enough to invest the time and re-test it.

j
guilty as charged  :)




More information about the Omaha-pm mailing list