G'day gabrielsousa,
After looking at your OP, and subsequent discussions in various parts of this thread,
I think your issues probably revolve around one or more of the following.
-
Not having a complete understanding of what the original data structure looks like.
-
Using a highly experimental feature (possibly in error).
-
Gaps in your knowledge of Perl's data structures and references.
Given the output you show, $jsonhash{results} is a hashref with a single key.
The complete hash looks something like this:
%jsonhash = (
results => {
single_key => {
subnet_id => undef,
...,
certname => 'adm003.siege.red',
...,
realm_name => undef
}
},
...
);
Here's how you might investigate that structure.
For illustrative purposes, and brevity (to show examples as one-liners),
I'm using this highly simplified version of your structure:
%h = (r => { x => { a => 1, b => 2 }})
and I've set up this alias:
$ alias perld='perl -Mstrict -Mwarnings -MData::Dump -E'
Here's a representation of %jsonhash:
$ perld 'my %h = (r => { x => { a => 1, b => 2 }}); dd \%h'
{ r => { x => { a => 1, b => 2 } } }
Here's a representation of $jsonhash{results}:
$ perld 'my %h = (r => { x => { a => 1, b => 2 }}); dd $h{r}'
{ x => { a => 1, b => 2 } }
Here's a representation of the single key in $jsonhash{results}:
$ perld 'my %h = (r => { x => { a => 1, b => 2 }}); dd $h{r}{x}'
{ a => 1, b => 2 }
As there's only one key, there's no need for a loop;
however, here it is using non-experimental code:
$ perld 'my %h = (r => { x => { a => 1, b => 2 }}); dd $_ for values %
+{$h{r}}'
{ a => 1, b => 2 }
As you can see, the result is the same as that without the loop.
Switching to an old version of Perl (v5.14.0), that implements the experimental feature,
you can see there's no change in the output:
$ perlbrew switch perl-5.14.0t
$ perld 'my %h = (r => { x => { a => 1, b => 2 }}); dd $_ for values $
+h{r}'
{ a => 1, b => 2 }
And, just for completeness, here it is again, using a more recent Perl (v5.24.0) and postfix dereference:
$ perlbrew switch perl-5.24.0t
$ perld 'my %h = (r => { x => { a => 1, b => 2 }}); dd $_ for values $
+h{r}->%*'
{ a => 1, b => 2 }
"how i convert the $value to an hash ?"
Firstly, consider whether you really want, or indeed need, to do this.
If you do, two options should now be obvious:
%{ $jsonhash{results}{single_key} }
$jsonhash{results}{single_key}->%*
Elsewhere, you were trying to get the value of 'certname'.
You don't need any convoluted referencing, dereferencing or temporary hashes;
it's immediately available as:
$jsonhash{results}{single_key}{certname}
See also:
"perlintro: Perl variable types"
(although this is an introductory text,
it has a lot of links to further information about complex data structures and references which I recommend);
keys,
values (recent - without experimental feature),
http://perldoc.perl.org/5.22.0//functions/values.html (older values doco with experimental feature);
Data::Dump.
|