Reputation: 557
I have simple perl script as below:
#!/usr/bin/perl
use strict;
use Data::Dumper;
my %x;
$x{"a"}="b";
$x{"b"}={'c'=>3,'d'=>4};
$x{"c"}={'e'=>{'f'=>5,'g'=>6},'h'=>{'i'=>7,'j'=>8}};
print Dumper(%x);
This is outputting me as below:
> ./temp.pl
$VAR1 = 'c';
$VAR2 = {
'e' => {
'g' => 6,
'f' => 5
},
'h' => {
'j' => 8,
'i' => 7
}
};
$VAR3 = 'a';
$VAR4 = 'b';
$VAR5 = 'b';
$VAR6 = {
'c' => 3,
'd' => 4
};
,
But my desired ouput is something different.so i tried the below code:
#!/usr/bin/perl
use strict;
use Data::Dumper;
my %x;
$x{"a"}="b";
$x{"b"}={'c'=>3,'d'=>4};
$x{"c"}={'e'=>{'f'=>5,'g'=>6},'h'=>{'i'=>7,'j'=>8}};
foreach (keys %x )
{
if(ref($x{$_}) eq "HASH")
{
print Dumper(\%{$x{$_}}).",";
}
else
{
print $x{$_}.",\n"
}
}
But this gives me an output as below:
> ./temp.pl
$VAR1 = {
'e' => {
'g' => 6,
'f' => 5
},
'h' => {
'j' => 8,
'i' => 7
}
};
,b,
$VAR1 = {
'c' => 3,
'd' => 4
};
but what i need is as below.i donot need VAR1 etc and also =>
in the output.I just need the keys and values with a space in between them
c {
'e' {
'g' 6,
'f' 5
},
'h' {
'j' 8,
'i' 7
}
},
a b,
b {
'c' 3,
'd' 4
}
All perl experts out there ,could anybody give me the right direction to print the output as i need it!
i got what i needed from perleone's suggestion. but i also tried the below staement:
my %y={"one"=>404,"two"=>\%x};
now if i do
print Dumper(\%y);
it gives me an output of :
> ./temp.pl
{
'HASH(0x807f08c)' undef
}
where did i go wrong here?
Upvotes: 2
Views: 205
Reputation: 4038
Have a look at the documentation. Use these settings:
$Data::Dumper::Pair = ' ';
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Terse = 1;
...
print Dumper( \%x );
Upvotes: 6