Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

nested foreach loop not working

by Annie17 (Initiate)
on Dec 21, 2021 at 09:58 UTC ( [id://11139798]=perlquestion: print w/replies, xml ) Need Help??

Annie17 has asked for the wisdom of the Perl Monks concerning the following question:

I have a hash "$VAR1 = [ { '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' } ]; I want to read the values into excel .Need help with the code .

my $samlattr = $responsetextall[$i][$j]{attributeStatements}; foreach my $result (@{ $sam +lattr } ) { #print "$result->{type}\n"; foreach my $var (@{ $samlattr{ +$result} } ) { print "$var->{type}\n"; $worksheet->write($r, 14,($var->{value +s}) );

Replies are listed 'Best First'.
Re: nested foreach loop not working
by choroba (Cardinal) on Dec 21, 2021 at 10:26 UTC
    $VAR1 is not a hash, it's a reference to an array (of hashes).

    It's not clear what part of $responsetextall[$i][$j]{attributeStatements} $VAR1 corresponds to. Can you clarify?

    #!/usr/bin/perl use warnings; use strict; my $arr = [ { 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 $hash (@$arr) { print "$hash->{type}\t"; print "$hash->{values}\n"; }
    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      $VAR1 contains the output of attributestatements.while printing the values of hash , it is giving the result of only the last hash of array not all. how to loop through it to print all the values

        I'm getting the following output:
        EXPRESSION [user.firstName] EXPRESSION [user.lastName]
        i.e. it contains all the "values". What output do you need instead?

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: nested foreach loop not working
by BillKSmith (Monsignor) on Dec 21, 2021 at 14:46 UTC
    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
      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' } ];

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11139798]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2024-04-16 21:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found