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

hi,can someone tell what is going on with the push stmt this code. Is it a hash? an array? And how is it storing the info?

sub load { my ($self,$file); open my $f,"<group.conf"; # opens config files my $last_group=''; while (my $line=<$f>) { chomp $line; next if length $line == 0; if($line =~/^group\d+/) { $last_group = $line; $config->{num_groups}++;#countgroups next; }; push @{ $config->{groups}->{$last_group} },$line; # collects each groups lower and upper bounds (BUT HOW?) }; } group.conf CONTAINS: group1 AG3333 .Y33 1999 HU4555 .T34 2000 group2 HV9069 .L28 2003 KA5333 .A12 2001 group3 KB XX

Replies are listed 'Best First'.
Re: PUSH Question
by ikegami (Patriarch) on Aug 13, 2008 at 17:19 UTC

    You can only ever push onto an array.

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

    could have been written as

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

    if that helps.

    Some reference material:
    perlref
    perllol
    References Quick Reference
    Dereferencing Syntax

    Update: Oops, those two snippets aren't the same due to autovivification.

    my $ref_to_array = $config->{groups}->{$last_group} push @$ref_to_array, $line; # In case $ref_to_array was autovivified, $config->{groups}->{$last_group} = $ref_to_array;

    Or vivify it explicity,

    my $ref_to_array = $config->{groups}->{$last_group} ||= []; push @$ref_to_array, $line;

    That kind of confuses the point, which was that the expression within the curlies returns an array reference, which is derefenced by @{}, resulting an array being passed to push.

      Im sorry,I should have said that the whole infix operator thing is very confusing. I really need basic help on this one. I dont know what gets stored in what. whats and array and whats a hash in this statement:
      push @{ $config->{groups}->{$last_group}
      I do understand the basic push and everything I see in the manual. Unfortunately, I cant find this type of syntax in the manual.
        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
Re: PUSH Question
by toolic (Bishop) on Aug 13, 2008 at 17:26 UTC
    Welcome to the Monastery.

    Please read Writeup Formatting Tips. Please reformat your original posting by deleting the br tags and adding code tags around your code snippet.

    A handy tool for debugging data structures is Data::Dumper:

    use Data::Dumper; print Dumper($config);