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}) );
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]
| [reply] [d/l] [select] |
|
| [reply] |
|
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]
| [reply] [d/l] [select] |
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
| [reply] [d/l] [select] |
|
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.
| [reply] |
|
"$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'
}
];
| [reply] [d/l] |
|
|
|
|