How to store arrays in hashes or objects?

From: Richard Heintze (sieg_heintze_at_yahoo.com)
Date: 10/29/03


Date: Tue, 28 Oct 2003 20:40:07 -0800 (PST)
To: beginners@perl.org

I had emailed this query out previously but since I
never saw my own email in the digest, I'm assuming
that it never made it to the beginners@perl.org list.
Please forgive me if it did and I did not see it (my
SPAM filter might have eaten it).

Question #1
-----------
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 = $#{@{$me->{component_titles}}}+1;

Something changed, I don't know what, and perl started
dying on the above statement with no error message or
explanation. I had to resort to this technique:

my @ComponentTitles = @{$me->{component_titles}};
my $nColumns = $#ComponentTitles+1 ;

This seems to work, but I don't like it because it
uses a supefluous deep copy. Can someone tell me how
to do it in one statement without a superfluous deep
copy?

Question #2
-----------

Incidently, I believe I'm storing an array here
instead of a reference to an array.

I don't really do anything to create to create this
array -- I just start storing elements like this:

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

Is there a better way to populate this array? Perhaps
with a declaration or something?

Question #3
-----------

I tried to create a second array and store it by
reference like this:

my $a = [];
$me->{a} = \$a;
$a->[0] = 1;
$a->[1] = 23;
etc...

But when I tried to retrieve the array, there was
nothing there:

print $me->{a}->[0];

I cured the problem by storing elements directly, but
I did not want to make a new copy of the array (I
wanted to store a reference to the original array).

  $ii = 0;
  $me->{a}->[$ii++] = $_ foreach (@{$a});

Why does not this work:
 $me->{a} = \$a;

  Thanks,
         Siegfried

__________________________________
Do you Yahoo!?
Exclusive Video Premiere - Britney Spears
http://launch.yahoo.com/promos/britneyspears/



Relevant Pages

  • Re: VB-101: Passing Arrays ByVal vs ByRef
    ... changed if an array is passed by Val. ... ' new reference ... As each function creates a new array object, ... 'secondArray' and 'secondArrayCopy'. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Need help with textboxes
    ... The JavaScript 1.5 Reference already states: ... All forms and their children are stored in an array ... use dot notation or object literals. ... No. Bracket property accessors allow their argument to be any string value. ...
    (comp.lang.javascript)
  • Re: Garbage Collection Issues in long-standing services
    ... the pinned array should get unpinned or ... The receive case is done quite well, I do create a 4K buffer and reuse it ... and remove the reference to my wrapper socket class, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to give selective access to the methods in a class?
    ... different memory locations at different times. ... The reference still leads to the ... So array resizing ... program pushes certain objects into the circular buffer, ...
    (comp.lang.java.programmer)
  • Re: Perlish map() function
    ... the reference material, the Array constructor was introduced in JavaScript ... evaluation would result in a ReferenceError. ... identifier shows that it is true. ...
    (comp.lang.javascript)