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.

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.

— Ken


In reply to Re: Convert string to hash by kcott
in thread Convert string to hash by gabrielsousa

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.