Why do I get a syntax error when I try to print a nested hash that has keys containing colons? - perl

I'm trying to print an element of a nested data structure:
$VAR1 = {
'SOAP:Body' => {
'ns1:MT_DF_AssetMaster_Response' => {
'SUBNUMBER' => {},
'ASSETCREATED' => {
'SUBNUMBER' => {},
'ASSET' => {},
'COMPANYCODE' => {}
},
'RETURN' => {
'PARAMETER' => 'timedependentdata',
'MESSAGE_V2' => {},
'ID' => 'BAPI1022',
'MESSAGE_V1' => 'HW5790',
'ROW' => '0',
'TYPE' => 'E',
'FIELD' => 'plate_no',
'LOG_NO' => {},
'MESSAGE_V3' => {},
'SYSTEM' => 'xxx',
'MESSAGE' => 'Invalid date transferred for field xxx:',
'MESSAGE_V4' => {},
'NUMBER' => '041',
'LOG_MSG_NO' => '000000'
},
'xmlns:ns1' => 'urn:ariba.com:xi:OnDemand:Asset',
'ASSET' => {},
'COMPANYCODE' => {}
}
},
'xmlns:SOAP' => 'http://schemas.xmlsoap.org/soap/envelope/',
'SOAP:Header' => {}
};
print "$data->{SOAP:Body}->{ns1:MT_DF_AssetMaster_Response}->{ASSETCREATED}=>{ASSET}\n";
But I get a syntax error:
syntax error at ./asset_creation.pl line 85, near "{SOAP:"
How can I fix this?

You must write something like this
my $asset = $data->{'SOAP:Body'}{'ns1:MT_DF_AssetMaster_Response'}{ASSETCREATED}{ASSET};
print %$asset ? "Asset is NOT empty\n" : "Asset is empty\n";

The following are the syntaxes for a hash lookup via reference:
REF->{IDENTIFIER}
REF->{EXPR}
ns1:MT_DF_AssetMaster_Response is neither a valid identifier, nor a valid expression. Replace
->{ns1:MT_DF_AssetMaster_Response}
with
->{'ns1:MT_DF_AssetMaster_Response'}

Perl allows an expression inside the subscripts for an array or a hash. For instance, a variable:
$array[ $i ]
Or the result of an addition:
$array[ $i + $offset ]
Or something
$array[ cos( $i ) ]
It's the same with a hash:
$hash{ $key }
The dot is still the string concatenation operator:
$hash{ $key . $prefix }
It's harder to spot when you have barewords smooshed together:
$hash{first.last}
And in this case, the : is still part of the namespace separator. Perl thinks you aren't done with this expression in the braces so it complains:
$hash{ SOAP:Body } # !!! Error

SOAP:Body must be quoted: The : may not be used without quoting as hash key:
print $data->{'SOAP:Body'}->{'ns1:MT_DF_AssetMaster_Response'}
You should also remove the " " around the variable name in your print statement. The hashref-dereferencing doesn't work otherwise. Your output would be something like
HASH(0x123456)->{SOAP:Body}->{ns1:MT_DF_AssetMaster_Response}
...but you may still have another error if line 85 isn't the print command.

Related

What difference does curly brace make in accessing an object

Not even sure if I used the right terminology..
This was the passed data.
$bug = bless({
'bug_id' => '25252',
'flag_types' => [
bless({
'name' => 'name1',
'flags' => [bless({
'id' => 488052,
}, 'Bugzilla::Flag')],
}, 'Bugzilla::FlagType'),
bless({
'name' => 'name2',
'flags' => [bless({
'id' => 488053,
}, 'Bugzilla::Flag')],
}, 'Bugzilla::FlagType'),
],
}, 'Bugzilla::Bug');
Why can I reference name with or without curly braces, but not the flags?
$_->flags gives error
Can't locate object method "flags"
my #isCrash = map {
print Dumper($_->name);
print Dumper($_->{name});
print Dumper($_->flags); # errors
print Dumper($_->{flags};
} #{ $bug->{flag_types} };
I get that flags is not a method, but why is it that I don't get such an error for name?
Your array contains objects of type Bugzilla::FlagType, they have a method name that "Returns the name of the flagtype".

Perl- Get Hash Value from Multi level hash

I have a 3 dimension hash that I need to extract the data in it. I need to extract the name and vendor under vuln_soft-> prod. So far, I manage to extract the "cve_id" by using the following code:
foreach my $resultHash_entry (keys %hash){
my $cve_id = $hash{$resultHash_entry}{'cve_id'};
}
Can someone please provide a solution on how to extract the name and vendor. Thanks in advance.
%hash = {
'CVE-2015-6929' => {
'cve_id' => 'CVE-2015-6929',
'vuln_soft' => {
'prod' => {
'vendor' => 'win',
'name' => 'win 8.1',
'vers' => {
'vers' => '',
'num' => ''
}
},
'prod' => {
'vendor' => 'win',
'name' => 'win xp',
'vers' => {
'vers' => '',
'num' => ''
}
}
},
'CVE-2015-0616' => {
'cve_id' => 'CVE-2015-0616',
'vuln_soft' => {
'prod' => {
'name' => 'unity_connection',
'vendor' => 'cisco'
}
}
}
}
First, to initialize a hash, you use my %hash = (...); (note the parens, not curly braces). Using {} declares a hash reference, which you have done. You should always use strict; and use warnings;.
To answer the question:
for my $resultHash_entry (keys %hash){
print "$hash{$resultHash_entry}->{vuln_soft}{prod}{name}\n";
print "$hash{$resultHash_entry}->{vuln_soft}{prod}{vendor}\n";
}
...which could be slightly simplified to:
for my $resultHash_entry (keys %hash){
print "$hash{$resultHash_entry}{vuln_soft}{prod}{name}\n";
print "$hash{$resultHash_entry}{vuln_soft}{prod}{vendor}\n";
}
because Perl always knows for certain that any deeper entries than the first one is always a reference, so the deref operator -> isn't needed here.

Hash not passing properly between functions

I set up a user hash in a function with the following,
push #{$profile{$index}{$infoName}}, $information
and print it using print Dumper(\%profile); index++; in the function it was set up it prints each of indexes
`$VAR1 = { '374' => { 'degree' => [ 'CS' ], 'birthdate' => [ '1973/12/13' ], 'gender' => [ 'M' ],...}
$VAR1 = { '375' => { 'degree' => [ 'CS' ], 'birthdate' => [ '1933/02/03' ], 'gender' => [ 'F' ],...}`
when i try to access this within another function's foreach loop using print "${$profile{$currIndex}{'gender'}}"; i get odd behaviour where the print returns an empty string and get some random numbers appear in the hash: '$VAR1 = { '4' => {}, '1' => {}, '3' => {}, '2' => {}, '378' => { 'birthdate' => [ '1961/03/29' ], 'gender' => ['F'],..}
How can i properly access the gender feild from within a loop?
push #{$profile{$index}{$infoName}}, $information;
print "${$profile{$currIndex}{'gender'}}";
I'm not even sure what the second line actually does. On my Ubuntu, perl produces an error: not a scalar reference.
What you want is, to print all array elements:
print "#{$profile{$currIndex}{'gender'}}\n";
or, to print the first:
print $profile{$currIndex}->{'gender'}->[0], "\n";
The leaf element is an array references and has to be dereferenced as such.
I'm not sure why you use there array references. In your sample data there are no multiple elements in the arrays. Probably you wanted to write simply this? -
$profile{$index}{$infoName} = $information;
...
print "$profile{$currIndex}{'gender'}\n";

Possible to detect hash with more than one key?

I am collecting data in a hash of hashes which looks like
$VAR1 = {
'502' => {
'user2' => '0'
},
'501' => {
'git' => '0',
'fffff' => '755'
},
'19197' => {
'user4' => '755'
}
};
The problem is in 501. Two keys may not occur. Is it possible to detect this?
Update
Fixed typo in hash.
If you are only going to store one key-value pair under each key of the main hash, why not use a 2-element array instead? That way you can check for existence before making each new insert, without needing to check the size of the hash or knowing what its keys are. The structure I'm proposing is this:
$VAR1 = {
'502' => [ 'user2', '0' ],
'501' => [ 'git', '0' ],
'19197' => [ 'user4', '755' ]
}
Assuming your hashref above is named $var :
my #bad = grep { scalar keys %{$var->{$_}} > 1 } keys %$var;
Results in an array of hash keys that have more than one hashref within them. Using your data above:
# perl test.pl
$VAR1 = {
'501' => {
'git' => '0',
'fffff' => '755'
},
'502' => {
'user2' => '0'
},
'19197' => {
'user4' => '755'
}
};
$VAR1 = '501';
Then you could access each element that is detected as bad with:
foreach my $key ( #bad ) {
# do something to or with $var->{$key}
}
keys(%{$VAR1{'501'}}) == 2 where the rest would be one.
Also, syntax error on that key, but I assume it's a typo.

perl - setting up conditions to find correct key in a hash

Problem:
Seeing exists argument is not a HASH or ARRAY element
Need help setting up several conditions to grab the right key.
Code: (I'm not sure also if my conditions are set up correctly. Need advice troubleshooting)
my $xml = qx(#cmdargs);
my $data = XMLin($xml);
my $size=0;
# checking for error string, if file not found then just exit
# otherwise check the hash keys for filename and get its file size
if (exists $data->{class} =~ /FileNotFound/) {
print "The directory: $Path does not exist\n";
exit;
} elsif (exists $data->{file}->{path}
and $data->{file}->{path} =~/test-out-XXXXX/) {
$size=$data->{file}->{size};
print "FILE SIZE:$size\n";
} else {
# print "Nothing to print.\n";
}
# print "$data";
print Dumper( $data );
My Data:
Data structure for xml file with FileNotFound:
$VAR1 = {
'file' => {},
'path' => '/source/feeds/customer/testA',
'class' => 'java.io.FileNotFoundException',
'message' => '/source/feeds/customer/testA: No such file or directory.'
};
Data structure for xml file found:
$VAR1 = {
'recursive' => 'no',
'version' => '0.20.202.1.1101050227',
'time' => '2011-09-30T02:49:39+0000',
'filter' => '.*',
'file' => {
'owner' => 'test_act',
'replication' => '3',
'blocksize' => '134217728',
'permission' => '-rw-------',
'path' => '/source/feeds/customer/test/test-out-00000',
'modified' => '2011-09-30T02:48:41+0000',
'size' => '135860644',
'group' => '',
'accesstime' => '2011-09-30T02:48:41+0000'
},
The interpreter is probably thinking you meant:
exists($data->{class}=~/FileNotFound/)
Try:
exists $data->{class} and $data->{class}=~/FileNotFound/
instead.