Hi Monks,

I'm stucked and can't find any solution. Since 3 days I am researching all kind of forums/documentation but I can't get any hint.

Here is my problem:

I'm requesting data from a REST-API and I'm receiving JSON-Format. Now I want to build a Dereference (e. g. $item->{meta}->{created}) dynamically but it does only work for the 1st level ($item->{userName}).

Maybe you can help relieving my headache?

Regards,
Flip

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Code Sniplet #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ my $data_structure = decode_json($client->responseContent()); print Dumper($data_structure); # Output of print Dumper($data_structure); $VAR1 = { 'Resources' => [ 'displayName' => 'User 1', 'userName' => 'user1@test.com', 'id' => '1234567', 'meta' => { 'created' => '2018-04-16T11:5 +7:19.376Z' } }, { 'displayName' => 'User 2', 'userName' => 'user2@test.com', 'id' => '1234568', 'meta' => { 'created' => '2018-04-16T11:5 +9:27.111Z' } }, { 'displayName' => 'User 3', 'userName' => 'user3@test.com', 'id' => '12345679', 'meta' => { 'created' => '2018-11-21T14:4 +9:33.821Z' } }, ], 'totalResults' => 3, 'itemsPerPage' => 50, 'startIndex' => 1, 'schemas' => [ 'urn:ietf:params:scim:api:messages:2.0:ListRe +sponse' ] }; # Normally @elements is filled by entries from a config-file # but for make it easy, I set the values here my @elements; push(@elements,"userName"); push(@elements,"displayName"); push(@elements,"meta.created"); foreach $item ( @{ $data_structure->{Resources} } ) { foreach $element (@elements) { # Convert a . to }->{ in order for meta.created to look like m +eta}->{created $element =~ s/\./}->{/g; # This works for all elements except of meta}->{created $deref = $item->{$element}; print "Convert: \$item->{" . $element . "}\t\t\t" . $deref . +"\n"; } # But this works deref = $item->{meta}->{created}; print "When use direct: \$item->{meta}->{created}\t" . $deref . "\ +n"; print "\n"; } #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Output: #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Convert: $item->{userName} user1@test.com Convert: $item->{displayName} User 1 Use of uninitialized value $deref in concatenation (.) or string at /h +ome/dczesak/cis4oci/cis4oci.pl line 291. Convert: $item->{meta}->{created} When use direct: $item->{meta}->{created} 2018-04-16T11:57:19.37 +6Z Convert: $item->{userName} user2@test.com Convert: $item->{displayName} User 2 Use of uninitialized value $deref in concatenation (.) or string at /h +ome/dczesak/cis4oci/cis4oci.pl line 291. Convert: $item->{meta}->{created} When use direct: $item->{meta}->{created} 2018-04-16T11:59:27.11 +1Z Convert: $item->{userName} user3@test.com Convert: $item->{displayName} User 3 Use of uninitialized value $deref in concatenation (.) or string at /h +ome/dczesak/cis4oci/cis4oci.pl line 291. Convert: $item->{meta}->{created} When use direct: $item->{meta}->{created} 2018-11-21T14:49:33.82 +1Z

2018-12-08 Athanasius changed <pre> to <p> tags around text


In reply to Dynamic Dereferencing by Flip76

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.