From the beginning...

$config is a hashref. It can be dereferenced to get the hash itself by prefixing it with %: %$config. Except there'll be precedence issues, so %{$config} is better. The values in the hash can be accessed via their keys like so ${$config}{groups}. But that's too longwinded. The "pointer" operator -> is used to shorten %{$config}{groups} to $config->{groups}.

The value associated with that particular key is another hashref, so that is accessed with another use of -> shortcut: $config->{groups}->{$last_group}.

That yields an arrayref that can also be shortcutted with the "pointer" operator to access individual elements, or using @{ ... } to access the dereferenced array, which is what you need for a push.

Hence (with typo's fixed):

push @{ $config->{groups}->{$last_group} }, $line;

Perhaps a little commented Perl will make easier to understand:

use Data::Dumper; my $last_group = "group_z"; my $line = "a line"; my $config = { # a hash... groups => { # ...of a hash... group_z => [ # ...of a list 2, 3, "Peter", "Pan", ], }, }; push @{ $config->{groups}->{$last_group} }, $line; print Dumper $config;

Outputs:

$VAR1 = { 'groups' => { 'group_z' => [ 2, 3, 'Peter', 'Pan', 'a line' ] } };

Have a look at perlref and perlreftut if you don't properly understand references yet, and then go on to read perldsc.

Update: Fixed a typo. Thanks ysth.


Unless I state otherwise, all my code runs with strict and warnings

In reply to Re^3: PUSH Question by FunkyMonk
in thread PUSH Question by emusic32

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.