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

david wright david_v_wright at yahoo.com
Tue May 27 12:43:13 PDT 2008


> >      Washington           $a1ref->[0]
> > 
> >            Seattle         $a1ref[0]->[0]
> > 
> >            Olympia         $a1ref[0]->[1]
> > 
> >            Tacoma          $a1ref[0]->[2]

this is as close to your original desired data structure as I can get:

assuming you mean, $a1ref->[0], $a1ref->[0]->[1]

use strict;
use warnings;
use Data::Dumper;


my $a1ref = ();
$a1ref->[0] = ['Washington'];
push( @{ $a1ref->[0]->[1] },  (qw/Seattle    Olympia   Tacoma /) );
warn Dumper $a1ref;


$VAR1 = [
          [
            'Washington',
            [
              'Seattle',
              'Olympia',
              'Tacoma'
            ]
          ]
        ];


+David

--- On Tue, 5/27/08, david wright <david_v_wright at yahoo.com> wrote:

> From: david wright <david_v_wright at yahoo.com>
> Subject: Re: [sf-perl] Pushing and Popping an array on the heap
> To: "San Francisco Perl Mongers User Group" <sanfrancisco-pm at pm.org>
> Date: Tuesday, May 27, 2008, 3:02 PM
> > I think the next structure fits better to your goal:
> >  
> > push @{$a1ref->{Washington}}, ("Seattle",
> > "Olympia", "Tacoma");
> > print $a1ref->{Washington}[0] . "\n";
> 
> I agree with Sergey,
> 
> push @{$a1ref->{Washington}}, ("Seattle",
> "Olympia", "Tacoma");
> warn Dumper $a1ref;
> 
> $VAR1 = {
>           'Washington' => [
>                             'Seattle',
>                             'Olympia',
>                             'Tacoma'
>                           ]
>         };
> 
> 
> There are a few indispensable resources I use for dealing
> with perl data structures:
> 
> perldoc perldsc (perldsc - Perl Data Structures Cookbook)
> 
> and the module (in perl base) Data::Dumper (an example as
> used above)
> 
> 
> +David
> 
> 
> --- On Tue, 5/27/08, Sergey Fetisov
> <sergey.fetisov at ask.com> wrote:
> 
> > From: Sergey Fetisov <sergey.fetisov at ask.com>
> > Subject: Re: [sf-perl] Pushing and Popping an array on
> the heap
> > To: "San Francisco Perl Mongers User Group"
> <sanfrancisco-pm at pm.org>
> > Date: Tuesday, May 27, 2008, 2:32 PM
> > 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_______________________________________________
> > SanFrancisco-pm mailing list
> > SanFrancisco-pm at pm.org
> > http://mail.pm.org/mailman/listinfo/sanfrancisco-pm
> _______________________________________________
> SanFrancisco-pm mailing list
> SanFrancisco-pm at pm.org
> http://mail.pm.org/mailman/listinfo/sanfrancisco-pm


More information about the SanFrancisco-pm mailing list