Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: nested foreach loop not working

by BillKSmith (Monsignor)
on Dec 21, 2021 at 21:42 UTC ( [id://11139809]=note: print w/replies, xml ) Need Help??


in reply to Re: nested foreach loop not working
in thread nested foreach loop not working

In Reply to your Chatterbox message:
Re [id://11139804 values' => 'user.firstName' ,it is an array, with the print statement it is giving ARRAY(0xx..) instead of the actual value.How to read the value

Your notation is not valid perl. In fact, I have no idea what you mean. (There is no key 'values' anywhere in your data) Your error message means that you are trying to print an array reference. You must dereference (Using References) it first.

Please modify my code to demonstrate your problem. We need code that we can actually run and duplicate your error. Post that demo as a reply to this post.

Bill

Replies are listed 'Best First'.
Re^3: nested foreach loop not working
by Annie17 (Initiate) on Dec 24, 2021 at 18:12 UTC

    Here is the data structure . I would like to print the values for 'name' and 'values'.

    "$VAR1 = [ { 'namespace' => 'urn:oasis:names:tc:SAML:2.0:attrname-forma +t:unspecified', 'name' => 'First Name', 'values' => [ 'user.firstName' ], 'type' => 'EXPRESSION' }, { 'namespace' => 'urn:oasis:names:tc:SAML:2.0:attrname-forma +t:unspecified', 'name' => 'Last Name', 'values' => [ 'user.lastName' ], 'type' => 'EXPRESSION' } ];

      Here is the SSCCE for that:

      #!/usr/bin/env perl use strict; use warnings; my $VAR1 = [ { 'namespace' => 'urn:oasis:names:tc:SAML:2.0:attrname-forma +t:unspecified', 'name' => 'First Name', 'values' => [ 'user.firstName' ], 'type' => 'EXPRESSION' }, { 'namespace' => 'urn:oasis:names:tc:SAML:2.0:attrname-forma +t:unspecified', 'name' => 'Last Name', 'values' => [ 'user.lastName' ], 'type' => 'EXPRESSION' } ]; for my $x (@$VAR1) { print "Name = $x->{name}, Values = $x->{values}[0]\n"; }

      See perldsc for more on using data structures like this.


      🦛

      Do you understand the change you have made to $VAR1? You have replaced the string 'user.firstName' with a reference to an array containing only one element (the original string). The error message you reported in your chatterbox message is exactlly what you should expect if you made only this change to my script. Note that my for $var loop no longer makes much sense because all the values no longer have the same structure. Use hippo's method to dereference just the two required values.

      Now show us your best effort to put these two values into your spread sheet.

      Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11139809]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-16 05:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found