[sf-perl] Pushing and Popping an array on the heap

Sergey Fetisov sergey.fetisov at ask.com
Tue May 27 11:32:40 PDT 2008


Hi,
 
The next works too:
push @$a1ref, ["Seattle", "Olympia", "Tacoma"];
print $a1ref->[0][0] . "\n";
 
But $alref->[0] is array and you can not push a scalar (e.g.
"Washington") there.
I think the next structure fits better to your goal:
 
push @{$a1ref->{Washington}}, ("Seattle", "Olympia", "Tacoma");
print $a1ref->{Washington}[0] . "\n";
 

-- Sergey
 
________________________________

From: sanfrancisco-pm-bounces+sergey.fetisov=ask.com at pm.org
[mailto:sanfrancisco-pm-bounces+sergey.fetisov=ask.com at pm.org] On Behalf
Of Dan Boger
Sent: Tuesday, May 27, 2008 10:11 AM
To: San Francisco Perl Mongers User Group
Subject: Re: [sf-perl] Pushing and Popping an array on the heap



On Tue, May 27, 2008 at 9:52 AM, Neil Heller <nheller at silcon.com
<blocked::mailto:nheller at silcon.com> > wrote:


	The problem is that I don't have a clue (nor am I able to find)
how to use (or the syntax involved to) "push" (or unshift) and "pop" (or
shift) values into and out of this described data structure.

I think (but can't be sure since you didn't post what you tried) that
you're running into a precedence issue.  If you type:

  push $a1ref[0]->[0], "value";

Push will complain since it wants an array as it's first argument, not
an array reference.  The way to get around it is to dereference the
array:

  push @{$a1ref[0]->[0]}, "value";

Same will work for any of the array manipulation commands (shift, pop,
unshift, splice, etc).

HTH!

Dan

-- 
Dan Boger 

________________________________

From: sanfrancisco-pm-bounces+sergey.fetisov=ask.com at pm.org
[mailto:sanfrancisco-pm-bounces+sergey.fetisov=ask.com at pm.org] On Behalf
Of Neil Heller
Sent: Tuesday, May 27, 2008 9:53 AM
To: 'San Francisco Perl Mongers User Group'
Subject: [sf-perl] Pushing and Popping an array on the heap



Some of the advantages of using an array are the push and pop functions.

 

The following instantiates a reference on the stack that points to an
(anonymous) empty array on the heap:

 

What I would like to do is:

 

1.       Instantiate a reference on the stack that points to an empty,
anonymous array on the heap.

2.       "push" a reference ($a2ref) into the original anonymous array
that points to a secondary, anonymous array

3.       "push" a series of references into the array referenced by
$a2ref

 

An example might look something like the following:

 

$a1ref = [undef];

(The values to be pushed are as follows);

 

     Washington           $a1ref->[0]

           Seattle         $a1ref[0]->[0]

           Olympia         $a1ref[0]->[1]

           Tacoma          $a1ref[0]->[2]

 

     Oregon               $a1ref->[1]

           Portland

           Salem

           Beaverton       

 

 

Finally, I would like to pop (or shift) values from $a1ref followed by
the individual members of the secondary array (such as $a1ref[0]->[0]).

 

The problem is that I don't have a clue (nor am I able to find) how to
use (or the syntax involved to) "push" (or unshift) and "pop" (or shift)
values into and out of this described data structure.

 

I appreciate any help I can get.

 

Neil Heller

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/sanfrancisco-pm/attachments/20080527/d86b5b2e/attachment-0001.html 


More information about the SanFrancisco-pm mailing list