How to get some values with a loop from Hashes of hashes in Perl - perl

I have a config.file to do some tests and i would like to get some values from this one also.
Here my config.file:
my $folder = 'E:\FOLDER\Test\WEB';
{
license => [ 'kit-licence.zip',
'kit-work.zip'
],
programs => [
#template society =>\%program_work
'VIKTOR DESCRIPTION PRODUCT' => {
name => 'VIKTOR ',
parameters => [
Count_id => '06 (Viktor)',
Birth_date => '1995-04-30',
Marriage_date => '2014-05-26',
Divorce_date => '2015-03-30',
Activities_folder => $folder.'\VIKTOR\independent worker',
Activities_format => 'Enterprise Format (V35)',
Description_File_from => $folder.'\VIKTOR\FILE\description.xlm',
]
},
'OLIVER NEW OBJECT' => {
name => 'OLIVER ',
parameters => [
Count_id => '06 (oliver)',
Birth_date => '1990-04-30',
Marriage_date => '2011-03-26',
Divorce_date => '2014-01-30',
Activities_folder => $folder.'\OLIVER\independent worker',
Activities_format => 'Enterprise Format (V35)',
Description_File_from => $folder.'\OLIVER\FILE\description.xlm',
]
},
]
};
My file test is following:
#test.pl
use Modern::Perl;
my $config = do 'work.conf';
use Data::Dumper;
say Dumper( $config );
To get parameters in Programs for Viktor for example, I can do this:
%programs = #{ $config->{programs} };
for my $prog (values %programs) {
my %param = #{ $prog->{parameters} };
for my $name (sort keys %param){
print $name, ': ', $param{$name},"\n";
}
}
But in my case, I want to be able to get parameters for every user. Here it's just for Viktor. I would like to get them for Oliver or for another user. For that, and to differentiate all users, I have to use the "template society" which is the name to differentiate every user. For example, for Viktor, it's: "VIKTOR DESCRIPTION PRODUCT". For Oliver: "OLIVER NEW OBJECT".
How can I do that?
Same thing for "License":
license => [ 'kit-licence.zip',
'kit-work.zip'
],
programs => [..
I would like to get the license by name of each one. For example, 'kit-license.zip'.
And not by "hard coding" like that:
use File::Spec::Functions qw/catfile/;
my $filename = catfile($::svn, ${$config->{license}}[0]);
my $filename1 = catfile($::svn, ${$config->{license}}[1]);
Perhaps in a loop, but I didn't find.
PS: Don't ask me why they are all divorced. I really don't know.

You're already doing a good job converting those array refs into hashes. But the values is making your life hard. You need the key and the value at the same time. You can use each to do that.
my %programs = #{ $config->{programs} };
while (my ($template_society, $value) = each %programs ) {
my %param = #{ $value->{parameters} };
print "$template_society\n";
for my $name ( sort keys %param ) {
print "\t", $name, ': ', $param{$name}, "\n";
}
}
This will produce the following output:
VIKTOR DESCRIPTION PRODUCT
Activities_folder: \VIKTOR\independent worker
Activities_format: Enterprise Format (V35)
Birth_date: 1995-04-30
Count_id: 06 (Viktor)
Description_File_from: \VIKTOR\FILE\description.xlm
Divorce_date: 2015-03-30
Marriage_date: 2014-05-26
OLIVER NEW OBJECT
Activities_folder: \OLIVER\independent worker
Activities_format: Enterprise Format (V35)
Birth_date: 1990-04-30
Count_id: 06 (oliver)
Description_File_from: \OLIVER\FILE\description.xlm
Divorce_date: 2014-01-30
Marriage_date: 2011-03-26
The each built-in returns both the key and the value of a hash per iteration, and undef once it's done. That's why you need to put it in a while loop.
If you don't like the each approach, you can also use keys instead of values to get the keys ($template_society) and use that to look up the appropriate value.
my %programs = #{ $config->{programs} };
foreach my $template_society (keys %programs ) {
my %param = #{ $programs{$template_society}->{parameters} };
print "$template_society\n";
for my $name ( sort keys %param ) {
print "\t", $name, ': ', $param{$name}, "\n";
}
}
This will give you the same output.
To get all your licence paths you need to store them in an array and use a loop to process your array ref into that array. The easiest and most concise way to do that is using map.
my #licences = map { catfile($::svn, $_ ) } #{ $config->{license} };
It's like a foreach loop, just shorter. The BLOCK is basically a function that gets the current iteration item in $_. It's essentially the same as the following, just more perlish.
my #licences;
foreach my $licence (#{ $config->{license} } ) {
push #licences, catfile($::svn, $licence );
}
Do not attempt to create variables like $foo1, $foo2 and so on dynamically. That will not work. See this1 for an explanation why.
Finally a word on $::svn: if you are in a package, you should put your code into a function and accept $svn as an argument. Working with globals or package variables from different packages is tricky and messy and you will at some point shoot yourself in the foot with it.
1: The normal document is currently broken, so I used archive.org to get it

Related

Array of Hashrefs: How to access based on hashref "column" values

I have an array of hashrefs built from a database using fethrow_hashref(). The data structure is built like so:
while (my $ref = $sth->fetchrow_hashref()) {
push #lines, $ref;
}
I sort the data in the query by program name ascending, so all of the references in the array are still in alphabetical order. Then, I go through each hash and find the value that is numerically equal to a '1'. I then take the caolumn name, and store it to compare to the rest of the hashrefs with that program name to ensure they all have a '1' in the same column.
my $pgm = "";
my $met_lvl = "";
my #devs = ();
my %errors = ();
my $error = "";
foreach my $line_ref (#lines) {
if ($pgm ne $line_ref->{"PROGRAM"}) {
if (#devs && $error) {
# print " Different number metal layers for $pgm: #devs \n";
$error = "";
}
#devs = ();
$pgm = $line_ref->{"PROGRAM"};
($met_lvl) = grep { $line_ref->{$_} == 1 } keys(%$line_ref);
push #devs, $line_ref->{"DEVICE"};
} elsif ($pgm eq $line_ref->{"PROGRAM"}) {
push #devs, $line_ref->{"DEVICE"};
my ($met_chk ) = grep { $line_ref->{$_} == 1 } keys(%$line_ref);
if ($met_chk ne $met_lvl) {
$errors{$line_ref->{"PROGRAM"}} = $line_ref->{"PROGRAM"};
$error = "YUP";
}
}
}
I'd like to be able to access the hashrefs individually, based on matching column names from the database. How can I access the hashrefs with "TEST" values for "PROGRAM" keys? I used Data::Dumper to provide an example of a few of the hashrefs I'd like to access based on "PROGRAM" value:
'PLM' => undef,
'SLM' => undef,
'QLM' => undef,
'DEVICE' => 'DEV1',
'TLM' => '1',
'DLM' => undef,
'ROUTING' => 'NORMAL',
'PROGRAM' => 'TEST'
};
$VAR455 = {
'PLM' => undef,
'SLM' => undef,
'QLM' => undef,
'DEVICE' => 'DEV2',
'TLM' => '1',
'DLM' => undef,
'ROUTING' => 'NORMAL',
'PROGRAM' => 'TEST'
};
$VAR456 = {
'PLM' => undef,
'SLM' => undef,
'QLM' => undef,
'DEVICE' => 'DEV3',
'TLM' => '1',
'DLM' => undef,
'ROUTING' => 'NON_STANDARD',
'PROGRAM' => 'EXP'
};
$VAR457 = {
'PLM' => undef,
'SLM' => undef,
'QLM' => undef,
'DEVICE' => 'DEV4',
'TLM' => '1',
'DLM' => undef,
'ROUTING' => 'NORMAL',
'PROGRAM' => 'FINAL'
};
I'd like to be able to access key values for the hashrefs which contain the same program name. I cannot even begin to figure out what type of operation to use for this. I assume map is the correct way to do it, but dereferencing the "PROGAM" value for each element (hashref) in the array is beyond the scope of my understanding. I hope I was able to define the problem well enough for you guys to be able to help.
Edit: The impetus for wanting to access hashrefs with the same 'PROGRAM" value is to be able to provide an output of selected values to print to a logfile. So, after I compare and find differences between those hashrefs with the same "PROGRAM" value, I want to access them all again, and print out the desired column values to the lofgile.
Looks like you need to exrtact subsets of your data (hashrefs) with the same PROGRAM name.
Can preprocess your data to build a hash with those names as keys, and arrayrefs (with suitable hashrefs) as values. Then process those groups one at a time.
use warnings;
use strict;
use feature 'say';
use Data::Dumper; # to print complex data below
... populate #lines with hashrefs as in the question or copy-paste a sample
# Build hash: ( TEST => [ hashrefs w/ TEST ], EXP => [ hashrefs w/ EXP ], ... )
my %prog_subset;
for my $hr (#lines) {
push #{ $prog_subset{$hr->{PROGRAM}} }, $hr;
# Or, using "postfix dereferencing" (stable from v5.24)
# push $prog_subset{$hr->{PROGRAM}}->#*, $hr;
}
foreach my $prog (keys %prog_subset) {
say "\nProcess hashrefs with PROGRAM being $prog";
foreach my $hr (#{ $prog_subset{$prog} }) {
say Dumper $hr;
}
}
(See postfix dereference)
Now %prog_subset contains keys TEST, EXP, FINAL (and whatever other PROGRAM names are in data), each having for value an arrayref of all hashrefs which have that PROGRAM name.
There are other ways, and there are libraries that can be leveraged, but this should do it.
OK! I found an example of this with the google machine. I replaced #lines = (); with $lines = [];. This allowed me to change the grep statement to (#found) = grep { $pgm eq $_->{PROGRAM} } #$lines;. Now the returned array is a list of the hashrefs that share the program name I'm looking for. Thanks for the help #zdim!

How do I decipher an array of hashes?

I totally got this question wrong. Am using the method from TMDB:
my #results = $search->find(id => 'tt0114694', source => 'imdb_id');
I thought the output was in JSON format, so that's what confused me, which kept me running in circles because I was looking at it all wrong.
Didn't realize the data below, from Dumper, was the actual hashes the I had to go through.
This is where I am running into a wall, So the data below is a hash with five keys. The fifth key, the I want, contains another array. It is that array I cannot read into. I try dereferencing that into a hash, and that is where I fail.
The code I am trying is:
foreach my $narray (#results){
print $narray->{"movie_results"};
my #newarray = $narray->{"movie_results"};
foreach my $otherarray (#newarray){
my %innerhash = $otherarray;
print %innerhash;
print "\n";
}
}
It will print out an array, but I am unable to read the hash in that array.
p.s. I had to format this output as code, or else it came out with no line breaks.
$VAR1 = {
'tv_season_results' => [],
'tv_results' => [],
'person_results' => [],
'tv_episode_results' => [],
'movie_results' => [
{
'adult' => bless( do{\(my $o = 0)}, 'JSON::PP::Boolean' ),
'vote_average' => '6.8',
'original_title' => 'Tommy Boy',
'vote_count' => 635,
'id' => 11381,
'release_date' => '1995-03-31',
'overview' => 'Party animal Tommy Callahan is a few cans short of a six-pack. But when the family business starts tanking, it\'s up to Tommy and number-cruncher Richard Hayden to save the day.',
'genre_ids' => [
35
],
'title' => 'Tommy Boy',
'video' => $VAR1->{'movie_results'}[0]{'adult'},
'poster_path' => '/g32WbO9nbY5ydpux5hIoiJkLEQi.jpg',
'original_language' => 'en',
'backdrop_path' => '/bZ4diYf7oyDVaRYeWG42Oify2mB.jpg',
'popularity' => '13.945'
}
]
};
You mention that you thought you'd get JSON output, but got something else. The module made a web request for you, received the JSON response, and translated that to a Perl data structure. That Perl version of the JSON is what you see in the dump.
A JSON object turns into a Perl hash, so that's what you see in the top level of the data structure. That's the single thing find returns (more on that in a moment):
Here's what you have, removing the outer foreach loop:
my #newarray = $narray->{"movie_results"};
foreach my $otherarray (#newarray){
my %innerhash = $otherarray;
print %innerhash;
print "\n";
}
The value in $narray->{"movie_results"} is an array reference. All references are scalars, and those scalars point to some data structure. When you assign that scalar to an array, you just end up with a one element array that's the same reference. Instead, you can
my $movie_results = $narray->{"movie_results"};
You then dereference that reference to treat it as an array:
foreach my $result ( #$movie_results ){ ... }
Or, the v5.24 postfix dereferencing way that I find slightly more pleasing since it reads better, especially when you skip the intermediate variable:
foreach my $result ( $movie_results->#* ){ ... }
foreach my $result ( $narray->{"movie_results"}->#* ){ ... }
That thing in $result is another hash reference.
References and data structures are about half of the content of Intermediate Perl, but there is also the Perl data structures cookbook (perldsc).
Improving your question a bit
You can help us a lot by showing us a complete, working demonstration of your problem. Here's what I cobbled together:
use v5.10;
use TMDB;
use Data::Dumper;
my $tmdb = TMDB->new( apikey => $ENV{TMDB_API_KEY} );
my #results = $tmdb->search->find(
id => 'tt0114694',
source => 'imdb_id'
);
say Dumper( \#results );
There was a question about the results of find. The documentation example shows it returning a list (well, the result being assigned to a named array, which implies that), but there's not actual documentation for find. It returns the decoded JSON from the response. Assigning it to a scalar (which will be a reference) works just fine too:
my $result = $tmdb->search->find(
id => 'tt0114694',
source => 'imdb_id'
);
say Dumper( $results );
The return value comes from TMDB::Sesssion::talk(), which is just this (or the empty list):
return $self->json->decode(
Encode::decode( 'utf-8-strict', $response->{content} ) );
That's not a big deal. That just means you don't need the outer foreach. That's not on you because the example in the docs tells you to do exactly what you did.
Now a better program
Putting all that together, here's a simple program pared down to just what you need:
use v5.10;
use TMDB;
my $tmdb = TMDB->new( apikey => $ENV{TMDB_API_KEY} );
my $result = $tmdb->search->find(
id => 'tt0114694',
source => 'imdb_id'
);
foreach my $item ( $result->{movie_results}->#* ) {
say "Title: $item->{title}";
}
Ref aliasing
There's an experimental feature called ref aliasing that lets you assign a reference to a reference of a named variable. It's an alias, so you can access and change the same data, just with a named variable. Something this is handy when you don't like
use v5.10;
use TMDB;
use experimental qw(refaliasing);
my $tmdb = TMDB->new( apikey => $ENV{TMDB_API_KEY} );
# response is a hash ref, so ref alias to a named hash
\my %result = $tmdb->search->find(
id => 'tt0114694',
source => 'imdb_id'
);
# the part you want is an array ref, so alias that
\my #movie_results = $result{movie_results};
# each item in the array is a hash ref, so alias those too
foreach \my %item ( #movie_results ) {
say "Title: $item{title}";
}
When dealing with reference, use the same syntax as if you weren't, but replace the name of the variable with a block that returns the reference.
%NAME -> %{ $ref } Or just %$ref
$NAME{...} -> ${ $ref }{...} Although $ref->{...} easier to read.
#NAME -> #{ $ref } Or just #$ref
$NAME[...] -> ${ $ref }[...] Although $ref->[...] easier to read.
Let's give $VAR a better name,
my $response = $VAR1;
This means you want
my $results = $response->{movie_results};
for my $result (#$results) {
for my $key (keys(%$result)) {
say "$key: $result->{$key}";
}
}
See
perlreftut
Perl Dereferencing Syntax
%newhash{$newkey} should be $newhash{$newkey}.

insert anonymous hash into anonymous hash for counting in a loop

I'm trying to count starts and stops of some services i keep track of in logs.
I'm not going to past here entire code, but my way of doing hash is this:
I'm passing those starts and stops into anonymous hash .
First I'm creating anonymous hash filled with keys and values (in my case $knot is a key an zeros are values). Next im replaqcing values with another hash.
My code looks like this:
foreach $knot (#knots){
chomp $knot;
$variable = $variable."$knot;0;";
$Services = {split(/;/,$variable)};
}
my $data =
{
Starts=>'0',
Stops=>'0',
};
foreach my $key (keys %$Services) {
$Services->{$key} = $data;
}
print Dumper $Services;
Printing shows:
$VAR1 = {
' knot1' => {
'Stops' => '0',
'Starts' => '0'
},
' knot2' => $VAR1->{' knot1'},
' knot3' => $VAR1->{' knot1'},
' knot4' => $VAR1->{' knot1'},
' knot5' => $VAR1->{' knot1'},
and so on. Is there a better way of doing this? My way if i'm correct is badly written because changing knot1 starts/stops changes every other knot values.
Counting is very simple in Perl, thanks to Autovivification. You can just create anonymous data structures on the fly, like so:
use Data::Dumper;
my %hash = ();
$hash{apple}{green}++;
$hash{apple}{red} ++;
$hash{pear}{yellow}++;
$hash{apple}{green}++;
$hash{apple}{red} ++;
$hash{apple}{green}++;
print Dumper(\%hash);
This will produce the desired structure for counting:
$VAR1 = {
'apple' => {
'green' => 3,
'red' => 2
},
'pear' => {
'yellow' => 1
}
};
This also works in loops using variables (here using a reference to a hash):
my $hash_ref = {};
for my $fruit (qw( apple pear apple peach apple pear )) {
$hash_ref->{$fruit}++;
}
print Dumper($hash_ref);
resulting in:
$VAR1 = {
'peach' => 1,
'pear' => 2,
'apple' => 3
};

Accessing and modifying a nested hash based on a dot separated string

I have a string as input, say apple.mango.orange = 100
I also have a hash reference:
$inst = {
'banana' => 2,
'guava' => 3,
'apple' => {
'mango' => {
'orange' => 80
}
}
};
I want to modify the value of orange using the input string. Can someone please help me how I could do this?
I tried splitting the string into (key, value) pair. I then did the following on the key string:
my $key2 = "\$inst->{".$key."}";
$key2 =~ s/\./}->{/g;
$$key2 = $value;
This does not work as intended. Can someone help me out here? I have read the Perl FAQ about not using a variable value as variable but I am unable to think of an alternative.
You are building string that consists of (buggy) Perl code, but you never ask Perl to execute it. ...but that's not the right approach.
sub dive_val :lvalue {
my $p = \shift;
$p = \($$p->{$_}) for #_;
$$p
}
my #key = split /\./, "apple.mango.orange";
dive_val($inst, #key) = $value;
or
use Data::Diver qw( DiveVal );
my #key = split /\./, "apple.mango.orange";
DiveVal($inst, map \$_, #key) = $value;
Not only is a symbolic reference a very bad idea here, it doesn't even solve your problem. You're building an expression in $key2, and just jamming another dollar sign in front of its name won't make perl execute that code. For that you would need eval, which is another bad idea
You can install and use the Data::Diver module, which does exactly this sort of thing, or you can simply loop over the list of hash keys, picking up a new hash reference each time and assigning the value to the element with the last key
The biggest issue is actually parsing the incoming string into a list of keys and a value. This code implements a subroutine apply which applies the implied operation in the string to a nested hash. Unless you are confident of your data, it needs some error checking addingto make sure each of the keys in the list exists. The Data:;Dumper output is just to demonstrate the validity of the result
use strict;
use warnings 'all';
use Data::Dumper;
my $inst = { 'banana' => 2, 'guava' => 3, 'apple' => { 'mango' => { 'orange' => 80 } } };
my $s = 'apple.mango.orange = 100';
apply($s, $inst);
print Dumper $inst;
sub apply {
my ($operation, $data) = #_;
my ($keys, $val) = $operation =~ /([\w.]+)\s*=\s*(\d+)/;
my #keys = split /\./, $keys;
my $last = pop #keys;
my $hash = $data;
$hash = $hash->{$_} for #keys;
$hash->{$last} = $val;
}
output
$VAR1 = {
'banana' => 2,
'apple' => {
'mango' => {
'orange' => '100'
}
},
'guava' => 3
};

Storing SOAP response as hash

I'm trying to store the response from a Perl (v5.12.4) SOAP call (SQL query) as a hash for further processing while maintaining the structure of the data. The columns will vary so I'd like to be able to build the column keys dynamically. Here is an example of the reply:
$VAR1 = {
'row' => [
#0
{
'column1' => '',
'column2' => 'f',
'column3' => '0',
'column4' => '',
'column5' => 'f',
'column6' => '0',
'column7' => 'f',
},
]
};
I'm interested in the column names and values. I've made some (comical) attempts at handling this, such as:
unless ($res->fault) {
$Data::Dumper::Indent = 3;
my (%reply) = $res->paramsall();
foreach my $keys (keys %reply) {
print "$keys\n";
}
}
This gets a "Reference found where even-sized list expected at script.pl line...". Any point in the right direction here is much appreciated.
Thanks
This :
$res->paramsall()
almost certainy returns a hashref.
So what you are doing is assigning the hashref to the 1st key of %reply.
my %reply = %{ $res->paramsall() };
Will probably work.
You should take a look at perlreftut for some methods to work with refrerence structures.
Dereferencing hashes with my %hash = %{ } makes a copy of the referenced hash.
You can work directly with hashrefs like this :
my $reply = $res->paramsall();
foreach my $key1 (keys %$reply) {
foreach my $key2 (keys %{ $reply->{$key1} }) {
print " $key2: " . $reply->{$key1}{$key2} . "\n";
}
}
In the 1st code example in your reply, this :
$reply{$key2}->{$key2}
should read :
$reply{$key1}{$key2}
since everything is already dereferenced.