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

Could any one tell me what the following statement is doing? Is it a hash reference?
push @jobs, {$key=>$value};
here $key & $value are simple scalars.

Replies are listed 'Best First'.
Re: array or hash reference
by PodMaster (Abbot) on Aug 11, 2004 at 07:05 UTC
    Yes, you're pushing a hash reference onto @jobs.
    C:\>perl -le"push @a,{};print for @a" HASH(0x2252c0) C:\>perl -le"push @a,[];print for @a" ARRAY(0x2252c0)
    `perldoc perlref'

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      Is this statement same as
      $hash{$key} = $value; push @jobs, \$hash;
        No. Even if you wrote that correctly
        $hash{$key} = $value; push @jobs, \%hash;
        it wouldn't be the same as push @jobs, { $key, $value }; because { $key, $value} returns a reference to an anonymous hash, while \%hash returns a reference to a named hash (%hash).

        MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
        I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
        ** The third rule of perl club is a statement of fact: pod is sexy.

Re: array or hash reference
by pelagic (Priest) on Aug 11, 2004 at 07:16 UTC
    When you are not sure about the data structure you are creating use Data::Dumper to document it for you:
    use strict; use Data::Dumper; my $key = 4711; my $value = "fubar"; my @jobs; push @jobs, {$key=>$value}; print Dumper \@jobs; ___OUTPUT___ $VAR1 = [ { 4711 => 'fubar' } ];

    pelagic
Re: array or hash reference
by Hena (Friar) on Aug 11, 2004 at 07:06 UTC
    I would say its pushing a hash reference into an array.
Re: array or hash reference
by Scarborough (Hermit) on Aug 11, 2004 at 13:24 UTC
    I think I recognise this from a question I replied to yesterday xml without cpan to get idea of this it was placed in a loop so you would end up with a data structure like this
    @jobs = ({'job1'='17:30'},{'job2'='17:31'}); #to access this data which is, I expect, what you need to know, you w +ould do something like this foreach $job(@jobs){ foreach $key (keys(%$job)){ print "$key = ".$key->{$key}."\n"; } }
    The reason I used this data structure in my orginal (very low dirty program) was to make sure that the hashes of data came out in the same order every time without using SAX filters on the XML the hashes where read from.
    UpdatedQouted the keys and values in the hashessorry!