On Feb 1, R. Joseph Newton said:

>vaishnavi krishnamurthy wrote:
>
>>Can anyone tell me how I can do the following - all the elements in my
>>array of unkown size should be initialised to the value 0 ? > > thanks,
>>vaish.
>
>No.  If the array is of uknown size, it is not possible to do *anything*
>to all elements.  Therefore no one can tell you how to do any particular
>thing.
>
>There are, on the other hand, ways to ensure that each member added to an
>array is initialized to some chosen value.  Assign 0 to each as it is
>added, for one.

You could use a Tie:: module to make array elements default to 0.

  package Tie::Array::Zero;

  require Tie::Array;
  @ISA = qw( Tie::StdArray );

  sub FETCH {
    my ($self, $idx) = @_;
    $self->[$idx] = 0 if not defined $self->[$idx];
    return $self->[$idx];
  }

  1;

Poof.  That's all you need.  Save it as TAZ.pm if you want.

  use TAZ;
  tie @x, 'Tie::Array::Zero';
  print $x[5];  # 0

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to