in reply to Re^2: array or hash reference
in thread array or hash reference

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.

Replies are listed 'Best First'.
Re^4: array or hash reference
by pelagic (Priest) on Aug 11, 2004 at 07:37 UTC
    Actually the data in @jobs would be the same at that moment. Although %hash could be accessed/manipulated separately;

    update  as answer to davido's post here below:
    That's actuallay what I meant: pushing the hashref onto the array (as in snippet 1) lets you change the "data" of the array by changing the hash referenced.
    While pushing the hash itself onto the array (as in snippet 2) takes a copy of the content of the hash and is afterwards independant of the hash.

    pelagic

      True, but you must beware the subtile bugs that could introduce if you're not careful. Consider the difference between the two following snippets:

      # snippet 1: use Data::Dumper; my @array; my %hash = ( 'one' => 1, 'two' => 2 ); foreach ( 1 .. 100 ) { push @array, \%hash; $hash{'one'}++; } print Dumper \@array; # snippet 2: use Data::Dumper; my %hash = ( 'one' => 1, 'two' => 2 ); my @array; foreach ( 1 .. 100 ) { push @array, { %hash }; $hash{'one'}++; } print Dumper \@array;

      Dave