Perl Grep issue in nested hash/array - perl

I want to remove values which contain "local" string in their value. here is my hash output (print Dumper ($hash)):
$VAR1 = {
'FARM_03' => [
'nfs01',
'nfs02',
'nfs03',
'localvmfs',
'localvmfs'
],
'FARM_07' => [
'nfs01',
'localvmfs',
'localvmfs'
],
'FARM_11' => [
'nfs01',
'localvmfs',
'localvmfs'
]
};
Hence I wrote below code in my script to omit the "local" entries:
foreach my $key ( keys %$hash )
{
#{ $hash->{key} } = grep { !/local/i } #{ $hash->{key} };
}
and here is the output after running above grep command:
$VAR1 = {
'FARM_03' => [
'nfs01',
'nfs02',
'nfs03',
'localvmfs',
'localvmfs'
],
'FARM_07' => [
'nfs01',
'localvmfs',
'localvmfs'
],
'FARM_11' => [
'nfs01',
'localvmfs',
'localvmfs'
]
'key' => []
};
It is not removing the "local" entries as well as it adds a new field 'key' => [].
Could you tell me what is wrong with my grep statement.
Thanks.

You have {key} where you mean {$key} (twice).

Related

Perl hash add values

I am trying to push values into hash. I want to add the values under 'par3'.
e.g.
$VAR1 = { 'obj1' => ['par1',
'par2',
'par3' => ['par4','par5','par6',....]]}
I should also be able to add elements into 'par3' in case 'obj1'-'par1'-'par2'-'par3' matches.
So far I have this, but I can't figure out how can I add "the second level" under 'par3':
push #{$a{$obj}},$par1,$par2,$par3
[ ... ] is a reference to an array. Array elements are scalars. So it is not possible to directly have the structure you seem to be requesting (ie. the par3 => [ ... ] pseudocode from your question). See perldsc
It's not obvious what you are trying to do but a couple of possible ideas might be to use a reference to a hash, or to replace the array with a hash:
use Data::Dumper;
$Var2a = {
'obj1' => [
'par1',
'par2',
{ 'par3' => undef, }
],
};
push #{ $Var2a->{obj1}[2]{par3} }, 'par4', 'par5', 'par6';
print Dumper $Var2a;
$Var2b = {
'obj1' => {
'par1' => undef,
'par2' => undef,
'par3' => undef,
},
};
push #{ $Var2b->{obj1}{par3} }, 'par4', 'par5', 'par6';
print Dumper $Var2b;
I think you want to add children to par3. As such, I think you want pars to have children. If so, you need a different data structure.
# Ordered
my $obj1 = [
{ name => 'par1', children => [ ] },
{ name => 'par2', children => [ ] },
{ name => 'par3', children => [
{ name => 'par4', children => [ ] },
{ name => 'par5', children => [ ] },
{ name => 'par6', children => [ ] },
] },
];
or
# Unordered
my $obj1 = {
par1 => { },
par2 => { },
par3 => {
par4 => { },
par5 => { },
par6 => { },
},
};
To append par7 to par3's children, you would use
# Ordered
use List::Util qw( first );
my $par7 = { name => 'par7', children => [ ] };
my $par3 = first { $_->{ name } eq 'par3' } #$obj1
or die( "par3 not found" );
push #{ $par3->{ children } }, $par7;
or
# Unordered
$obj1->{ par3 }{ par7 } = { };

Issue accessing Hash in perl

I have a Hash of following structure in perl -
my %testHash = (
KeyL1 => {
KeyLL1 => {
KeyLLL1 => [1,2],
KeyLLL2 => [2,3],
},
KeyLL2 => {
KeyLLL1 => [1,2],
KeyLLL2 => [2,3],
},
KeyLL3 => {
KeyLLL1 => [1,2],
KeyLLL2 => [2,3],
},
},
KeyL2 => {
KeyLL1 => {
KeyLLL1 => [1,2],
KeyLLL2 => [2,3],
},
KeyLL2 => {
KeyLLL1 => [1,2],
KeyLLL2 => [2,3],
},
KeyLL3 => {
KeyLLL1 => [1,2],
KeyLLL2 => [2,3],
},
},
);
Now, when I am trying to access it the following way, I am getting 'undef' as a result
my %tempHash = $testHash{'KeyL1'};
print Data::Dumper::Dumper($tempHash{'KeyLL1'});
print Data::Dumper::Dumper($tempHash{'KeyLL1'}{'KeyLLL1'});
Result --
$VAR1 = undef; $VAR1 = undef;
Please point to me what am I doing wrong. I am pretty new to perl.
The value of $testHash{'KeyL1'} is a hashref, not a hash.
Hashrefs are scalars. my %tempHash = is not expecting a scalar.
You need to dereference it:
my %tempHash = %{$testHash{'KeyL1'}};
Also, you could do it this way if its just about viewing the structures.
Also try:
print Dumper $testHash{KeyL1} ;
print Dumper $testHash{KeyL1}{KeyLL1} ;
print Dumper $testHash{KeyL1}{KeyLL1}{KeyLLL1} ;
Output:
%_Host#User> ./hash.pl
$VAR1 = {
'KeyLL1' => {
'KeyLLL2' => [
2,
3
],
'KeyLLL1' => [
1,
2
]
},
'KeyLL2' => {
'KeyLLL2' => [
2,
3
],
'KeyLLL1' => [
1,
2
]
},
'KeyLL3' => {
'KeyLLL2' => [
2,
3
],
'KeyLLL1' => [
1,
2
]
}
};
$VAR1 = {
'KeyLLL2' => [
2,
3
],
'KeyLLL1' => [
1,
2
]
};
$VAR1 = [
1,
2
];
%_Host#User>

dereference xml::simple output which is complex data structure that is a hash of an array of hashs

I am trying to parse xml using xml::simple .Here is the output of xml::simple
$VAR1 = {
'soapenv:Body'=>[
{
'ns1:queryResponse'=>[
{
'ns1:result'=>[
{
'ns1:done'=>['true'],
'ns1:queryLocator' => [
{
'xsi:nil' => '1',
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance'
}
],
'ns1:size' => [
'60'
],
'ns1:records' => [
{
'ns2:RefundTransactionTime' => [
'2013-09-12T13:17:18.000-08:00'
],
'xmlns:ns2' => 'http://object.abccom/',
'ns2:MethodType' => [
'CreditCard'
],
'ns2:Gateway' => [
'Chase Paymentech'
],
'ns2:Type' => [
'Electronic'
],
'ns2:RefundDate' => [
'2013-09-12T00:00:00'
],
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'ns2:Status' => [
'Processed'
],
'ns2:Id' => [
'2c92c0f8410f4d9a014113d2170a2e17'
],
'xsi:type' => 'ns2:Refund',
'ns2:AccountId' => [
'2c92c0f9410f55ee0141132b6c936e15'
],
'ns2:Amount' => [
'99'
],
'ns2:CreatedDate' => [
'2013-09-12T13:17:17.000-08:00'
]
},
{
'xmlns:ns2' => 'http://object.abccom/',
'ns2:MethodType' => [
'CreditCard'
],
'ns2:Type' => [
'External'
],
'ns2:RefundDate' => [
'2013-10-12T00:00:00'
],
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'ns2:Status' => [
'Processed'
],
'ns2:Id' => [
'2c92c0f8410f4d9a0141145bfbb61a9b'
],
'xsi:type' => 'ns2:Refund',
'ns2:AccountId' => [
'2c92c0f8410f4d8701411411a9ad79b7'
],
'ns2:Amount' => [
'477.74'
],
'ns2:CreatedDate' => [
'2013-09-12T15:47:54.000-08:00'
],
'ns2:Comment' => [
'16 Payment Exception - Chargeback'
]
}
]
}
],
'xmlns:ns1' => 'http://api.abccom/'
}
]
}
],
'xmlns:soapenv' => 'http://schemas.xmlsoap.org/soap/envelope/'
};
I am using below code :
#!/usr/bin/env perl
use strict;
use Data::Dumper;
use XML::Simple qw(:strict);
my $data = XMLin('Refund.xml', forcearray=>1, keyattr=>[] );
print "Reference type in data is : ", ref($data), "\n";
print Dumper($data);
#Try to access the values
my $records=$data->{"soapenv:Body"}->[0]->{"ns1:queryResponse"}->[0]->{"ns1:result"}->
[0]->{"ns1:records"};
foreach my $record ( #{ $records } ) {
print $record->{"ns2:RefundTransactionTime"};
print "\n";
}
print Dumper($data) generates hash reference containing array of hashes.
I want to format the hash reference generated above to the array of array references format as shown below:
[
[
"AccountId",
"Id",
"Amount",
"CreatedDate",
"Comment",
"Status",
"Type",
"RefundDate",
"MethodType"
],
[
"2c92c0f8410f4d8701411411a9ad79b7",
"2c92c0f8410f4d9a0141145bfbb61a9b",
"477.74",
"2013-09-12T15:47:54.000-08:00",
"16 Payment Exception - Chargeback",
"Processes",
"External",
"2013-10-12T00:00:00",
"CreditCard"
],
[
"2c92c0f9410f55ee0141132b6c936e15",
"2c92c0f8410f4d9a014113d2170a2e17",
"99",
"2013-09-12T13:17:17.000-08:00",
"",
"Processed",
"Electronic",
"2013-09-12T00:00:00",
"Chase Paymentech"
]
],
Any help is greatly appreciated. Thank you
The return value ARRAY(0x1f9fca98) indicates that $record->{"ns2:RefundTransactionTime"} contains an array reference. You probably want the first element: $record->{"ns2:RefundTransactionTime"}->[0].

Hash merge/concatenation

this is a dump of my hashes: %hash1
$VAR1 = {
abc => {
123 => [
'xx',
'yy',
'zy'
],
456 => [
'ab',
'cd',
'ef'
]
}
};
and the second one: %hash2
$VAR2 = {
def => {
659 => [
'wx',
'yg',
'kl'
],
456 => [
'as',
'sd',
'df'
]
},
abc => {
987 => [
'lk',
'dm',
'sd'
]
}
};
Now I want to merge these two hashes in a new hash, but if a key is duplicated (here 'abc'), the values should be appended, not replaced, so the keys should remain unique, and all the values should be retained as well. How can this be done in Perl?
The output should be as follows:
$VAR1 = {
def => {
659 => [
'wx',
'yg',
'kl'
],
456 => [
'as',
'sd',
'df'
]
},
abc => {
987 => [
'lk',
'dm',
'sd'
],
123 => [
'xx',
'yy',
'zy'
],
456 => [
'ab',
'cd',
'ef'
]
}
};
Use the CPAN modules Hash::Merge or Hash::Merge::Simple. The first is highly configurable and the second is very simple to use.
for my $x (keys(%h2)) {
for my $y (keys(%{ $h2{$x} })) {
push #{ $h1{$x}{$y} }, #{ $h2{$x}{$y} };
}
}
For the sample data provided, the following would perform the merging you describe:
my %merged = map {
$_ => {
%{$a{$_} // {}},
%{$b{$_} // {}}
}
} ( keys %a, keys %b );
Test:
use strict;
use warnings;
use Data::Dump 'dd';
my %a = (
abc => {
123 => [
'xx',
'yy',
'zy'
],
456 => [
'ab',
'cd',
'ef'
]
}
);
my %b = (
def => {
659 => [
'wx',
'yg',
'kl'
],
456 => [
'as',
'sd',
'df'
]
},
abc => {
987 => [
'lk',
'dm',
'sd'
]
}
);
my %merged = map {
$_ => {
%{$a{$_} // {}},
%{$b{$_} // {}}
}
} ( keys %a, keys %b );
dd \%merged;
# {
# abc => {
# 123 => ["xx", "yy", "zy"],
# 456 => ["ab", "cd", "ef"],
# 987 => ["lk", "dm", "sd"],
# },
# def => { 456 => ["as", "sd", "df"], 659 => ["wx", "yg", "kl"] },
# }
sub merge_hashes {
my ($h1, $h2) = #_;
foreach my $key (keys %$h2) {
if (!exists $h1->{$key} || ref($h1->{$key}) ne 'HASH' || ref($h2->{$key}) ne 'HASH') {
$h1->{$key} = $h2->{$key};
}
else {
merge_hashes($h1->{$key}, $h2->{$key});
}
}
}
merge_hashes(\%hash1, \%hash2);

Search in LDAP with conditions?

When I do
#!/usr/bin/perl -w
use strict;
use Net::LDAP;
use Data::Dumper;
my $dn="...";
my $password="xxx";
my $ldap = Net::LDAP->new('example.com') or die "$#";
my $mesg = $ldap->bind($dn, password => $password);
if ($mesg->code) { die "uuuu $mesg"; }
$mesg = $ldap->search(
base => "dc=example,dc=com",
filter => "(name=LIST)",
);
print Dumper $mesg;
I get
$VAR1 = bless( {
'parent' => bless( {
...
}, 'Net::LDAP' ),
'entries' => [
bless( {
'changes' => [],
'changetype' => 'modify',
'asn' => {
'objectName' => 'CN=LIST,OU=test group,OU=M,OU=I,DC=example,DC=com',
'attributes' => [
{
'type' => 'objectClass',
'vals' => [
'top',
'group'
]
},
{
'type' => 'cn',
'vals' => [
'LIST'
]
},
{
'type' => 'member',
'vals' => [
'CN=user1,OU=BaseUsers,DC=example,DC=com',
'CN=user2,OU=BaseUsers,DC=example,DC=com',
]
},
...
where I would only like to output those from member that have in their object
objectCategory: CN=Person,CN=Schema,CN=Configuration,DC=example,DC=com
Does anyone know how to do that?
foreach my $entry (#{$mesg->{'entries'}})
{
my $match = 0;
my $name = $entry->{'asn'}->{'objectName'};
foreach my $attr (#{$entry->{'asn'}->{'attributes'}})
{
if('member' eq $attr->{'type'})
{
foreach my $val (#{$attr->{'vals'}})
{
if($val =~ /^CN=.*,CN=.*,CN=.*,DC=example,DC=com$/)
{
$match = 1;
last;
}
}
}
}
if($match)
{
print $name;
}
}
For your example data above this will return no matches since none of the "members" match the search pattern you specified. Also I wasn't sure if you wanted to output the object name (as per my code) or the matching string. If the latter you don't need $match, simply put a print in the innermost block.