in reply to nested foreach loop not working

I recommend that you postpone attempting to write to Excel until you have learned how to dereference your data structure. The following changes to your code should help. After you have figured out how to select the required data and print it to the screen, you can ask for help in writing that data to Excel.
use strict; use warnings; #my $samlattr = $responsetextall[$i][$j]{attributeStatements}; my $samlattr = [ { 'namespace' => 'urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified', 'name' => 'First Name', 'values' => 'user.firstName', 'type' => 'EXPRESSION' }, { 'namespace' => 'urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified', 'name' => 'Last Name', 'values' => 'user.lastName', 'type' => 'EXPRESSION' } ]; foreach my $result ( @{$samlattr} ) { #print "$result->{type}\n"; print "\n\n"; #foreach my $var ( @{ $samlattr{$result} } ) { foreach my $var ( sort keys %$result ) { #print "$var->{type}\n"; print "$var => $result->{$var}\n"; #$worksheet->write( # $r, 14, # ( $var->{values}) ); } }

OUTPUT:

name => First Name namespace => urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified type => EXPRESSION values => user.firstName name => Last Name namespace => urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified type => EXPRESSION values => user.lastName
Bill

Replies are listed 'Best First'.
Re^2: nested foreach loop not working
by BillKSmith (Monsignor) on Dec 21, 2021 at 21:42 UTC
    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

      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