in reply to enforcing list context
It might be simpler to just have a master data item with a "length" value, however, since this would require constant time to retrieve rather than linear time, and would allow you to significantly simplify the above function.use strict; use warnings; my $r = 0; $r = { rest => $r, first => 'c' }; $r = { rest => $r, first => 'b' }; $r = { rest => $r, first => 'a' }; print join " ", value($r), scalar value($r); sub value { my $self = $_[0]; if (wantarray) { my @value; while ($self) { push @value, $self->{'first'}; $self = $self->{'rest'}; } return @value; } my $c; while ($self) { $c++; $self = $self->{'rest'}; } return $c; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: enforcing list context
by billh (Pilgrim) on Apr 26, 2006 at 20:38 UTC |