On Sun Mar 01 2009 @  1:04, Octavian Râsnita wrote:
> From: "prasath_linux" <[email protected]>
>> Hi,
>>
>> Is there any possible function to print the associative array.  We
>> have print_r() function in PHP to display the associative array.
>> Likwise is there any function in perl to print associative array.
>>
>> Thanks in advance.
>>
>
>
> You can use:
>
> use Data::Dump qw(pp);
>
> my %hash = (a => 1, b => 2);
>
> print pp(\%hash);
>
> Octavian

Just a note - Data::Dump is not a core module, so the first step would be
to install it and then try Octavian's program.

You could also use the less pretty, but very functional, Data::Dumper
(which is a core module, so no extra installation is required):

  use Data::Dumper;

  my %hash = (a => 1, b => 2);

  print Dumper \%hash;

Even more generally, if you want to work on everything in a hash, you can
iterate over its keys or key/value pairs using the 'keys' or 'each' keyword:

  foreach my $key (keys %hash) {
    print "$key => $hash{$key}\n";
  }

  while ( my ($key, $value) = each %hash ) {
    print "$key => $value\n";
  }

Hope this helps, T

-- 
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to