How to dereference only hash of hash of array in Perl? - perl

{
'A' => ['B'], //hash of array
'C' => {'D' => [ 'E']} //hash of hash of array
}
When try to parse hash of hash of array. getting "Not a HASH reference" error. Even I tried used exists and defined keywords to avoid this error. But result is same error.
From above i need to only iterate hash of hash of array.
foreach my $keys (keys %$hash){
print "$keys";
if (defined $hash->{$keys}->{maptype}){
foreach my $array_element ( #{$hash->{$keys}->{'D'}} ) {
print "$array_element");
}
}
}

Not sure if this is best practice, but:
my $hash = { A => ['B'], C => {D => ['E'], F => [qw(G H I)]}, J=>42 };
for my $key (keys %$hash) {
if (ref $hash->{$key} eq 'HASH') {
for my $subkey (keys %{$hash->{$key}}) {
say "$key => $subkey => [", join(",", #{$hash->{$key}{$subkey}}), "]";
}
}
}
outputs
C => D => [E]
C => F => [G,H,I]

Related

Iterate through values of a HASH and convert to comma separated strings

I want to iterate through the values of a big hash, and if any of the values of that hash are keys, I want to convert it into a comma separated list which can be parsed in 'query_form'.
Right now from the data below I have:
name=Bob&surname=Whitbread&customerErrors=HASH(Xa456) (for example)
Here's what I have so far:
sub convertArgsToQueryString {
my $class = shift;
my $args = shift;
return unless ($args && ref($args) eq 'HASH');
foreach my $key (values %$args) {
if (ref($key) eq 'HASH') {
# change to a comma separated list
}
}
my $dummyURL = URI->new('', 'http');
$dummyURL->query_form(%$args);
return $dummyURL->query;
}
Data:
my $data = {
'name' => 'Bob',
'surname' => 'Whitbread',
'customerErrors' => {
'error1' => 'paymentError',
'error2' => 'addressError'
},
};
Query Form:
name=Bob&surname=Whitbread&customerErrors=paymentError,addressError
This will do what you want
print join ",", values %{$data->{customerErrors}},"\n";
Although I would suggest, rather than error1 as hash keys, you'd be better off with an array:
my $data = {
'name' => 'Bob',
'surname' => 'Whitbread',
'customerErrors' => [ 'paymentError', 'addressError' ],
};
Scaling that out to be generic, you will find the ref function to be helpful:
foreach my $key ( keys %$data ) {
print "$key is a ", ref $data->{$key},"\n";
if ( ref $data->{$key} eq 'HASH' ) {
print join ",", values %{$data->{$key}};
}
else {
print $data -> {$key},"\n";
}
}
Or tersely:
print join "\&", map { #join iterated on &
join "=", $_, #join paired values on =
ref $data->{$_} eq 'HASH' #ternary to check reference type
? values %{ $data->{$_} } #extract values if HASH
: $data->{$_} #extract just value if not.
} keys %$data; #iterate keys of data
Which gives as output:
name=Bob&customerErrors=addressError=paymentError&surname=Whitbread

Perl get value of a key

I have been stuck in trying to create an array of keys (example_com,example_ca ..etc) if they are set to 1, I have tried using for loop and foreach loop, but keep getting ARRAY# error.
$VAR1 = [
{
'example_com' => '1',
'example_ca' => '1'
}
];
Thanks
This will be because you have an array containing a hash. The array is one element long.
So you 'get' to the hash, by dereferencing element zero.
Thus:
my $hash_ref = $VAR1->[0];
print join "\n", keys %{$hash_ref},"\n";
foreach my $key ( keys %{$VAR1->[0]} ) {
print "$key => $VAR1->[0]{$key}\n";
}
Exactly for you source data:
my #array_of_keys = ();
for( keys %{ $VAR1->[0] } ) {
push #array_of_keys, $_ if $VAR1->[0]{ $_ } eq '1';
}
print "Keys with 1: #array_of_keys";
An expanded example of how to get an array of keys if you have multiple hashes in your container array:
my $VAR1 = [
{
'example_com' => '1',
'example_ca' => '1',
'not_set' => '0'
},
{
'EXAMPLE_com' => '1',
'EXAMPLE_ca' => '1',
'NOT_SET' => '0',
}
];
my #arrayOfHashes = #{$VAR1};
foreach my $array (#arrayOfHashes)
{
my #onlyOnes;
my #arrayOfKeys = sort keys %{$array};
foreach my $key (#arrayOfKeys)
{
next if ($array->{$key} ne 1);
push #onlyOnes, $key;
}
print "\nKey names:\n";
foreach my $key (#onlyOnes)
{
print "$key\n";
}
}
output:
Key names:
example_ca
example_com
Key names:
EXAMPLE_ca
EXAMPLE_com

perl hash of hashes - for eacy key group inner keys and respective values

I have a hash of hashes retrieved from json and I am trying to flatten hash, group and print values. That is, for each key1 group all keys and respective values.Trying to achieve this in perl using below code but, it is printing individual values each ID and names but not grouped values. Could you please help me how to group and print keys and values.
Data:
VAR1= {
'ID1' => {'Name1' => {'Address1' => {'Mod1' => ['pol1']}},
Name2' => {'Address1' => {'Mod2' => ['pol2']}}},
'ID2' => {'Name3' => {'Address3' => {'Mod3' => ['pol3']}}},
'ID3' => {'Name4' => {'Address4' => {'Mod4' => ['pol1, pol2, pol3']}}}
}
Expected Output:
ID1 => [Name1,Address1,mod1(pol1), Name2,Address1,mod2(pol2)],
ID2 => [Name3,Address3,mod3(pol3)],
ID3 => [Name4,Address4,mod4(pol1,pol2,pol3)]
Code1:
for my $id (#id_list) {
foreach my $item (sort (slurp(\%{$new{$id}}))) {
my $data = join(',', #$item);
print "data.. $data\n";
push(#results,$data);
}
}
print map {"$_"} #results;
Code2:
foreach my $id (sort keys %new){
foreach my $name (keys %{$new{$id} }) {
foreach my $address (keys %{$new{$id}{$name}}) {
foreach my $mod (keys %{$new{$id}{$name}{$address}) {
foreach my $value (#{$new{$id}{$name}{$address}{$mod}}) {
my $sp = ":";
my $reasons = join(',',$id,$name,$address,$mod,$value.$sp);
push (#rea, $reasons);
}
}
}
}
}
With such a nested structure you want to go systematically, like in your "Code2".
use warnings;
use strict;
use feature qw(say);
my %id_list = (
ID1 => {
Name1 => {'Address1' => {'Mod1' => ['pol1']}},
Name2 => {'Address1' => {'Mod2' => ['pol2']}}
},
ID2 => { 'Name3' => {'Address3' => {'Mod3' => ['pol3']}} },
ID3 => { 'Name4' => {'Address4' => {'Mod4' => ['pol1, pol2, pol3']}} }
);
my (%flattened, #ar, $modvals);
foreach my $id (sort keys %id_list){
foreach my $name (keys %{$id_list{$id} }) {
foreach my $address (keys %{$id_list{$id}{$name}}) {
foreach my $mod (keys %{$id_list{$id}{$name}{$address}}) {
$modvals = "$mod(" .
join(',', #{$id_list{$id}{$name}{$address}{$mod}})
. ')';
$modvals =~ s/,$//; # strip the last comma
push #ar, $name, $address, $modvals;
}
}
}
$flattened{$id} = [ #ar ];
#ar = ();
}
say "$_ => #{$h{$_}}" for sort keys %flattened;
Output
ID1 => Name1 Address1 Mod1(pol1) Name2 Address1 Mod2(pol2)
ID2 => Name3 Address3 Mod3(pol3)
ID3 => Name4 Address4 Mod4(pol1, pol2, pol3)
This generates the string for mod3 as (pol1, ...) -- as given in input, as a single string. If that is chaged to a list of pol in input, the spaces between pol's will be gone in output.
A hash with id keys and array ref with respective entries is generated.

Print the value in Hash of Hash for two different keys in Perl

Below is a hash in Perl:
my %hash = (
'episode1' => {
'when' => '08.13.97',
'airdate' => '08.13.97',
'episodenumber' => '101',
'id' => '103511',
'title' => 'Cartman Gets an Anal Probe',
'available' => 'true'
},
'episode2' => {
'when' => '08.20.97',
'airdate' => '08.20.97',
'episodenumber' => '102',
'id' => '1035156',
'title' => 'Weight Gain 4000',
'available' => 'true'
}
);
I want to print the "id" of both episodes,but the below code is not working:
foreach my $key1 ( keys %hash ) {
foreach my $key2 ( keys %{$hash{$key1}} ) {
print "$hash{$key1}{$key2}{id}\n";
}
}
Please help.
The problem is that you're trying to print something that doesn't exist: There is no value that matches $hash{$key1}{$key2}{id}.
Try this code, which prints out the value in the hash of hashes that has the key "id":
use strict;
use warnings;
for my $episode (keys %hash){
print "$hash{$episode}{id}\n";
}
103511
1035156
Try using map:
my #ids = map { $hash{$_}{"id"} } sort keys %hash;
Or if you still need the results as a hash:
my %ids_by_key = map { ($_, $hash{$_}{"id"}) } keys %hash;
Try this:
foreach my $key1 ( keys %hash ) {
print "$hash{$key1}{id}\n";
}
or
foreach my $key1 ( keys %hash ) {
foreach my $key2 ( keys %{$hash{$key1}} ) {
print "$hash{$key1}{$key2}\n" if($key2 eq 'id');
}
}

not able to access hash of hash of array values

I have written the following code in Perl. The code is reading a pdb file and getting some values. Ignore the top part of the code,where everything is working perfect.
Problem is in the sub-routine part, where I try to store arrays in the hash3 with model as key another key position
the array values can be accessed inside the if condition using this :
$hash3{$model}{$coordinates}[1].
but when I go out of all foreach loop and try to access the elements I only get one value.
Please look at the end foreach loop and tell me is it the wrong way to access the hash values.
The pdb file I am using can be downloaded from this link http://www.rcsb.org/pdb/download/downloadFile.do?fileFormat=pdb&compression=NO&structureId=1NZS
#!/usr/bin/perl
open(IN,$ARGV[0]);
my #phosphosites;
my $model=1;
my %hash3;
while(<IN>)
{
#findmod(#line);
#finddist;
#findfreq;
if((/^MODRES/) && (/PHOSPHO/))
{
#line=split;
push(#phosphosites, $line[2]);
#print "$line[4]";
}
foreach $elements (#phosphosites){
if(/^HETATM\s+\d+\s+CA\s+$i/)
{
#line1=split;
#print "$line1[5]";
#print "$line1[6] $line1[7] $line1[8]\n";
push(#phosphositesnum, $line1[5]);
}
}
$pos=$line1[5];
#findspatial(\#line,\#line1);
}
my #ori_data=removeDuplicates(#phosphositesnum);
sub removeDuplicates {
my %seen = ();
my #vals = ();
foreach my $i (#_) {
unless ($seen{$i}) {
push #vals, $i;
$seen{$i} = 1;
}
}
return #vals;
}
$a=(#phosphosites);
print "$a\n";
print "#phosphosites\n";
print "#ori_data\n";
close(IN);
open(IN1,$ARGV[0]);
my (#data)=<IN1>;
spatial(\#ori_data);
sub spatial {
my #spatial_array1=#{$_[0]};
foreach $coordinates(#spatial_array1)
{
$model=1;
{foreach $data1(#data){
if($data1=~ m/^HETATM\s+\d+\s+CA\s+[A-Z]*\s+[A-Z]*\s+$coordinates/)
{
#cordivals=split(/\s+/,$data1);
push #{ $sphash{$model} },[$cordivals[6], $cordivals[7], $cordivals[8]];
$hash3{$model}{$coordinates}= \#cordivals;
#print "$model $coordinates $hash3{$model}{$coordinates}[6] $hash3{$model}{$coordinates}[7] $hash3{$model}{$coordinates}[8]\n";
#print "$model $sphash{$model}[$i][0] $sphash{$model}[$i][1] $sphash{$model}[$i][2]\n";
}
elsif($data1=~ m/^ENDMDL/)
{
$model++;
}
#print "$model $coordinates $hash3{$model}{$coordinates}[6] $hash3{$model}{$coordinates}[7] $hash3{$model}{$coordinates}[8]\n";
}
}
}
#foreach $z1 (sort keys %hash3)
# {
# foreach $z2(#spatial_array1){
# print "$z1 $z2";
# print "$hash3{$z1}{$z2}[6]\n";
# print "$z2\n";
# }
# }
}
After using the Data::Dumper option it is giving me this kind of output
$VAR1 = {
'11' => {
'334' => [
'HETATM',
'115',
'CA',
'SEP',
'A',
'343',
'-0.201',
'-2.884',
'1.022',
'1.00',
'99.99',
'C'
],
'342' => $VAR1->{'11'}{'334'},
'338' => $VAR1->{'11'}{'334'},
'335' => $VAR1->{'11'}{'334'},
'340' => $VAR1->{'11'}{'334'},
'343' => $VAR1->{'11'}{'334'},
'336' => $VAR1->{'11'}{'334'}
},
'7' => {
'334' => $VAR1->{'11'}{'334'},
'342' => $VAR1->{'11'}{'334'},
'338' => $VAR1->{'11'}{'334'},
'335' => $VAR1->{'11'}{'334'},
'340' => $VAR1->{'11'}{'334'},
'343' => $VAR1->{'11'}{'334'},
'336' => $VAR1->{'11'}{'334'}
},
'2' => {
'334' => $VAR1->{'11'}{'334'},
'342' => $VAR1->{'11'}{'334'},
...
Change:
#cordivals=split(/\s+/,$data1);
to:
my #cordivals=split(/\s+/,$data1);
What seems to be happening is that all the hash elements contain references to the same array variable, because you're not making the variable local to that iteration.
In general, you should use my with all variables.