On Tue, 28 Oct 2003 20:40:07 -0800, Richard Heintze wrote:
> I have an array stored in an object and I trying to
> compute the length of the array. This seemed to work
> initially:
> 
> my $nColumns = [EMAIL PROTECTED]>{component_titles}}}+1;

$#array gives to the index of the _last_ element.  If you want the length
of the array (ie. the _number_ of elements), you should evaluate the array
in a scalar context;

  my $length = @array; # or @{ $array } for array references

> Something changed, I don't know what, and perl started
> dying on the above statement with no error message or
> explanation.

Really?  Did you include strict, warnings and diagnostics?

  #!/usr/bin/perl
  #
  use strict;
  use warnings;
  use diagnostics;

Always use these (at least strict and warnings) when developing Perl code.

> I had to resort to this technique:
> 
> my @ComponentTitles = @{$me->{component_titles}};
> my $nColumns = $#ComponentTitles+1 ; 

Could have been written as:
  my $nColumns = @{ $me->{component_titles} };

> I don't really do anything to create to create this
> array -- I just start storing elements like this:
> 
>   $me->{component_titles}[0] = "xyz";

You could always initialise an array reference;

  $me->{component_titles} = [];

And then you could assign values to it:

  $me->{component_titles}->[0] = 'xyz';

Are you sure you want to assign data directly onto an element index?  Have
you considered push()'ing data onto the array?

> I tried to create a second array and store it by
> reference like this:
> 
> my $a = [];
> $me->{a} = \$a;

$a is _already_ an array reference.  No need to make a reference of a
reference. :-)

  my @array    = ();
  my $arrayref = [EMAIL PROTECTED];


-- 
Tore Aursand <[EMAIL PROTECTED]>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to