[thelist] Perl references

David Kaufman david at gigawatt.com
Wed Feb 19 12:42:01 CST 2003


Chris Marsh <chris at ecleanuk.com> wrote...

> for ($i = 0; $i < @week; $i++) {
> $week[$i]{DATE} = [ @date ];
> &incrementDate(\@date, 1);
> }
>
> sub incrementDate {
> my($date, $type) = @_;
> $date[3]++;
> return;
> }
>
> I want &incrementDate to reference original array @date and increment
> index 3 for each pass of the loop, but I'm getting no errors but no
> incrementing either.

ok, yes i think your problems are a bit subtle.

when $date arrives in incrementate(), it is a reference to @date.  the
correct way to increment it's element 3 is:

$date->[3]++; # note th arrow which references an element of an arrayref

also, (i don'tknow if this is what you wanted to do, but) in the loop, the
idiom: [ @date ] returns a reference to a *copy* of @date, not a reference
to @date, because [ ( ...) ] is the syntax for constructing an anonymous
arrayref.

-dave




More information about the thelist mailing list