Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

unshifting by reference

by palkia (Monk)
on Dec 16, 2011 at 00:25 UTC ( [id://943858]=perlquestion: print w/replies, xml ) Need Help??

palkia has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I got this var $this->{M} which is an array & $this->{S} which is a scalar.
(those are part of a bigger structure which I can't alter)
The problem is when I try unshift($this->{M},$this->{S});
it says Type of arg 1 to unshift must be array (not hash element)
it says the same even when I avoid using strict.
so I tried unshift(@this->{M},$this->{S});
and it said Global symbol "@this" requires explicit package name
so I tried unshift(@($this->{M}),$this->{S});
and it said Scalar found where operator expected
yeah I'm sure I'm missing something really oblivious,
but I'm getting tired of the sight of my own tail.

your thoughts ?
Thx

Replies are listed 'Best First'.
Re: unshifting by reference
by Eliya (Vicar) on Dec 16, 2011 at 00:29 UTC
    unshift(@($this->{Memory}),$this->{Senses});

    To dereference the array ref, you need curlies, not parentheses, i.e. @{$this->{Memory}}

    my $this = { Memory => ["foo"], Senses => "bar", }; unshift @{$this->{Memory}}, $this->{Senses}; use Data::Dumper; print Dumper $this; __END__ $VAR1 = { 'Senses' => 'bar', 'Memory' => [ 'bar', 'foo' ] };
      Thx it works (obvious it was ^^).
      But I don't et it, I thought that the "->" dereferences, doesn't it ?
      when I did print $this->{N}[0]{Value}; it worked ok, so whats the difference ?
      Why didn't I needed to dereference there ?
      and whats "Dumper" is this really required ? (I prefer to avoid module dependencies)
      Thx again

        The thing is that unshift needs an array (syntactically), not an array reference. And the way to get an (entire) array from a reference is to say @$aref, or @{ $something->{that}{returns}[1]{aref} } for more complex expressions.  To address a single element of the array, you'd say $aref->[$index].

        (The Data::Dumper module is to easily print out complex data structures. I just used it for demo purposes — you of course don't need it to unshift the value.)

        -> is dereferencing a hash that has nothing to do with your question.

        Your question is about dereferencing the array referenced by $this->{N}.

        -> is dereferencing a hash that has nothing to do with your question.

        Your question is about dereferencing the array referenced by $this->{N}.

Re: unshifting by reference
by kejohm (Hermit) on Dec 16, 2011 at 04:56 UTC

    On a side note, as of Perl v5.14, shift, unshift, push, and pop can now take a reference to an unblessed array as their first argument, which will be automatically dereferenced, although it is considered experimental.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://943858]
Approved by Eliya
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-25 15:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found