Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Limiting array size?

by ned (Initiate)
on Jul 05, 2001 at 23:40 UTC ( #94227=perlquestion: print w/replies, xml ) Need Help??

ned has asked for the wisdom of the Perl Monks concerning the following question:

I know it goes against the grain of Perl in many ways, but my life would be much simpler at the moment if I could just put a cap on the size an array could grow to. Is there any quick and easy way to do this short of continually checking the size of the array and popping off elements once it gets to the size you want it?

Replies are listed 'Best First'.
Re: Limiting array size?
by japhy (Canon) on Jul 06, 2001 at 00:04 UTC
    Make your own array. This one specifically croaks when the size limitation would be exceeded. You can change it so that pushing to it would remove elements from the beginning if needed.
    package Tie::Array::MaxLen; use Carp; sub TIEARRAY { my ($class, $size) = @_; croak "size must be positive" if $size <= 0; return bless [ $size, [] ], $class; } sub check ($$) { my ($size, $idx) = @_; croak "index $idx out of bounds" if $idx >= $size or ($idx < 0 and -$idx > $size); } sub FETCH { my ($self, $idx) = @_; check($self->[0], $idx); return $self->[1][$idx]; } sub STORE { my ($self, $idx, $value) = @_; check($self->[0], $idx); return $self->[1][$idx] = $value; } sub FETCHSIZE { my ($self) = @_; return scalar @{ $self->[1] }; } sub STORESIZE { # maybe you want this, maybe not my ($self, $size) = @_; croak "size must be positive" if $size <= 0; $self->[0] = $size; $#{ $self->[1] } = $size-1; } sub CLEAR { my ($self) = @_; $self->[1] = []; } sub POP { my ($self) = @_; return pop @{ $self->[1] }; } sub PUSH { my $self = shift; check($self->[0], @_ + @{ $self->[1] }); push @{ $self->[1] }, @_; } sub SHIFT { my ($self) = @_; return shift @{ $self->[1] }; } sub UNSHIFT { my $self = shift; check($self->[0], @_ + @{ $self->[1] }); unshift @{ $self->[1] }, @_; } sub EXISTS { # remove if not 5.6+ my ($self, $idx) = @_; check($self->[0], $idx); exists $self->[1][$idx]; } sub DEFINED { my ($self, $idx) = @_; check($self->[0], $idx); defined $self->[1][$idx]; } sub SPLICE { my $self = shift; my $off = @_ ? shift : 0; $off += $self->[0] if $off < 0; my $len = @_ ? shift : $self->[0] - $off; $len += $self->[0] if $len < 0; check($self->[0], $off + $len); splice(@{ $self->[1] }, $off, $len, @_); }
    As "complete" as that looks, I've not tested it. It seems like it should work though. Sample use:
    tie my(@five), 'Tie::Array::MaxLen', 5; push @five, 10, 20, 30, 40; push @five, 50; push @five, 60; # QUACK

    japhy -- Perl and Regex Hacker
Re: Limiting array size?
by bikeNomad (Priest) on Jul 05, 2001 at 23:44 UTC
    What would you do when the array tried to grow bigger?

    Anyway, you should look at Tie::Array, specifically Tie::StdArray. I think you can override STORE and STORESIZE to do what you want (depending on what it is).

Re: Limiting array size?
by lhoward (Vicar) on Jul 05, 2001 at 23:45 UTC
    You could do it transparently by using ties (once you implemented the tie). Depending on how you want to limit it your STORE sub would check the current array size and act apropriately if it would cause an overflow (reject the insert, throw out some data already in the array, die, etc...). If you're wanting to do this to implement a limited size cache there are already modules out there to do that (Tie::Cache, Tie::Cache::LRU, etc..).
(jptxs)Re: Limiting array size?
by jptxs (Curate) on Jul 05, 2001 at 23:48 UTC
    again, depending on exactly how and why you're implamenting this, you could just write a sub that you call each time you want use the array that checks and rejects it if the length is unacceptable. The Tie ideas are more elegant, but if you can't use them this could work too...

    We speak the way we breathe. --Fugazi

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://94227]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2022-05-26 14:43 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (93 votes). Check out past polls.