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

Hi All,
I have the following problem.....

#I have two hashes

my %self; my %abldcache;

#I then populate the abldcache array and make self #reference it

$self{abldcache} = \%abldcache;

#I then use DataDumper to dump this to a file

print OUTFILE Data::Dumper->Dump([%self->{abldcache}], [qw(self->{abldcache})]);

#This works and gives me the following example of output...

$self->{abldcache} = { 'test1' => [], 'test2' => [ 'element1' + 'element2', 'element3' + ] };

#However, I want to produce this in the following format...

$self->{abldcache}->{'test1'} = [], $self->{abldcache}->{'test2'} = [ 'element1' + 'element2', 'element3' + ]

Could anyone tell me how to restructure my hashes so that Datadumper will give me this format???
Thanks in advance for any help you can give.

Edited 2005-02-11 by Ovid

Replies are listed 'Best First'.
Re: Using Data Dumper to represent 3 hashes
by Roy Johnson (Monsignor) on Feb 11, 2005 at 20:04 UTC
    You don't need to restructure your hashes, you just need to pass separately to Dumper each thing you want listed separately.
    my %self; my %abldcache = (test1 => [], test2 => [qw(one two three)]); $self{abldcache} = \%abldcache; use Data::Dumper; sub per_key { my ($name, $href) = @_; my @hkeys = keys %$href; ([@$href{@hkeys}], [map {"$name\{$_}"} @hkeys]) } print Data::Dumper->Dump(per_key('$self{abldcache}', $self{abldcache} +)), "\n"; __END__ # Yields: $self{abldcache}{test1} = []; $self{abldcache}{test2} = [ 'one', 'two', 'three' ];
    Close enough?

    Caution: Contents may have been coded under pressure.

      Cheers,
      Thats exactly what I needed :-)

Re: Using Data Dumper to represent 3 hashes
by Roy Johnson (Monsignor) on Feb 11, 2005 at 19:34 UTC
    Please put code tags around your code. That means you put this line:
    <code>
    before the first line of code, and this line:
    </code>
    after the last line of code. Then PerlMonks will format it properly so we can read it.

    How do I post a question effectively? may be helpful.


    Caution: Contents may have been coded under pressure.