perl hash ref operator is working weirdly - perl

I m having a single array ref consisting a list of hash refs.
Here is the code:
use strict;
use warnings;
my $arrayRef = [
{
'URL' => 'http://example.com/1.jpg',
'ORD' => '1',
},
{
'URL' => 'http://example.com/2.jpg',
'ORD' => '2',
},
{
'URL' => 'http://example.com/3.jpg',
'ORD' => '3',
},
];
print $arrayRef->[0]->{URL},"\n"; # http://example.com/1.jpg
print $arrayRef->[0]{URL},"\n"; # http://example.com/1.jpg
Since it is an hash ref i am using -> operator to get a value, but i m getting the same value without using the -> operator is it fine?

It's valid, a kind of syntactic sugar, and it refers to the same item. The Perl references tutorial writes about this

Related

Config::IniFiles hash behaves different than manually written hash

I am loading a config file, which ends up as an embedded hash, with Config::IniFiles. After that, I want to modify the resulting hash by, for some keys, bringing its values one level up. In the example below, I am aiming for this as a result:
$VAR1 = {
'max_childrensubtree' => '7',
'port' => '1984',
'user' => 'someuser',
'password' => 'somepw',
'max_width' => '20',
'host' => 'localhost',
'attrs' => {
'subattr2' => 'cat',
'topattr1' => 'cat',
'subattr2_1' => 'pt',
'subattr1' => 'rel'
},
'max_descendants' => '1000'
};
So for the keys params and basex at the highest level, I want to move its contents (key-value pairs) to the highest level - and remove the items themselves. In short:
(
a => {
'key1' => 'ok',
'key2' => 'hello'
}
)
turns into
(
'key1' => 'ok',
'key2' => 'hello'
)
The strange thing is that what I am trying to do does not work on a hash built from a read INI file, but it does work with a manually inserted hash. In other words, this works:
#!/usr/bin/perl
use utf8;
use strict;
use warnings;
use Data::Dumper;
my %ini = (
'params' => {
'max_width' => '20',
'max_childrensubtree' => '7',
'max_descendants' => '1000'
},
'attrs' => {
'topattr1' => 'cat',
'subattr1' => 'rel',
'subattr2' => 'cat',
'subattr2_1' => 'pt',
},
'basex' => {
'host' => 'localhost',
'port' => '1984',
'user' => 'someuser',
'password' => 'somepw'
}
);
&_parse_ini(\%ini);
sub _parse_ini {
my $ref = shift;
foreach (('params', 'basex')) {
foreach my $k (keys %{$ref->{$_}}) {
$ref->{$k} = $ref->{$_}->{$k};
}
delete $ref->{$_};
}
print Dumper($ref);
}
But this does not:
#!/usr/bin/perl
use utf8;
use strict;
use warnings;
use Data::Dumper;
use Config::IniFiles;
# Load config file
tie my %ini, 'Config::IniFiles', (-file => $ARGV[0]);
&_parse_ini(\%ini);
sub _parse_ini {
my $ref = shift;
foreach (('params', 'basex')) {
foreach my $k (keys %{$ref->{$_}}) {
$ref->{$k} = $ref->{$_}->{$k};
}
delete $ref->{$_};
}
print Dumper($ref);
}
The input ini file for this example would be:
[params]
max_width = 20
max_childrensubtree = 7
max_descendants = 1000
[attrs]
topattr1 = cat
subattr1 = rel
subattr2 = cat
subattr2_1 = pt
[basex]
host = localhost
port = 1984
user = admin
password = admin
I have been looking in the documentation and on SO for similar issues but have found none. It appears that the hashes are identical (Config::IniFiles doesn't seem to add something specific), so I have no idea why it works for 'manual' hashes, and not for read-in ones.
The two hashes are not identical at all, although they may appear to be from the point of view of the data they contain.
The first one is a regular hash. You can do whatever you like with it.
The second one is a tied hash. It becomes an object of Config::IniFiles, but with a hash like interface. So whilst it appears to be a hash, the package can override the methods for storing or fetching information in the hash however it likes.
In this particular case, it looks like Config::IniFiles will only store a new key value in the hash if the value is hash ref. So you can't flatten out the tied hash as you want. Instead you'll have to create a new hash and copy the data in to it to do what you want.

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

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.

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.