I have a Perl data structure, loaded from a json, with Data::Dumper looking like this:
$VAR1 = {
'Stat' => [
{
'statCfgFile' => 'statcfg_0001.json',
'statid' => 1,
'status' => 'running',
'something' => 'other'
},
{
'statCfgFile' => 'statcfg_0002.json',
'statid' => 2,
'status' => 'running'
'something' => 'other'
}
]
}
From another dataset, I get a statid to read the hash, but the given id does not match the array key, therefore I need to go into deep, reading the statid property.
Of course, I could loop through the array, but with a large array list, that might hit performance.
Is there a way to directly access the array element by the statid stored in the hash?
As the data model is in my hands and currently in development:
Would it be better not to use an array, but also a hash with the statid as the naming element?
The short answer is no, you can't pull a value out of an array based on a criteria without iterating over the array. But there are ways to write it without using a loop.
Say the data structure is $data and you want to pull hashes where statid equals $id_to_find.
my #matching_hashes = grep {$_->{statid} = $id_to_find}} #{$data->{Stat}};
If only the first hash matching the ID is relevant, you could use the core module List::Util function first, which does the same thing as grep but only returns the first match instead of a list. It would be faster than grep because it stops iterating over the array as soon as it finds one match.
use List::Util 'first';
my $matching hash = first {$_->{statid} = $id_to_find}} #{$data->{Stat}};
As you indicated, using hash lookups is much faster than list operations. You could create a hash index of array adresses. Note this still requires you iterate over the array once.
# create the index;
my %index;
my #array = #{$data->{Stat}};
for my $address (0..$#array) { # "$#array" is the last element of #array
my $hash = $array[$address];
my $id = $hash->{statid};
$index{$id} = $address; # now you can use an ID to get the array address
}
# use the index
my $id_to_find = 42;
my $wanted_array_address = $index{$id_to_find};
my $matching_hash = $data->{Stat}->[$wanted_array_address];
If the statid field is unique then I suggest you use something more like this
{
1 => 'statcfg_0001.json',
2 => 'statcfg_0002.json',
}
The hash includes more data than shown. I extend the example
Then you need a hash instead of just a string value for each statid
{
1 => {
statCfgFile => 'statcfg_0001.json',
statid => 1,
status => 'running',
something => 'other',
},
2 => {
statCfgFile => 'statcfg_0002.json',
statid => 2,
status => 'running',
something => 'other',
},
}
Related
I try to figure out how to select explicit key and values of a hash.
my $hash = query('select id, name, required from table')->hashes
My output when I dump is:
var1 = bless ([
{
'name' => value,
'id' => value,
'required' => value
}.....
])
What I want is to get the follow output:
var1 = bless ([
{
'required' => value
}...
])
After that I want to compare if the index == index of another array.
You removed the class name from bless, but I guess it's Mojo::Collection. Use it's map method to iterate over the elements:
my $required = $hash->map(sub { required => $_->{required} });
# Untested.
Also, using the name $hash for something that's a collection of hashes is confusing.
I don't understand your last sentence about the index. If you want to extract the $index-th element, you can use
my $hash = $required->to_array->[$index];
or to get directly the value
my $value = $required->to_array->[$index]{required};
Once you get a hash reference, from whatever means, you access the keys like this:
$hash->{required}; # etc.
See perlref for more information. Also read perldsc and perlreftut.
I am trying to merge two hashes. But they are return values from functions. How can I dereference the return values inline? I don't want to use extra variables such as my $pos = makePos();
use v5.8.8;
use strict;
use warnings;
sub _makePos
{
my $desc= {
pos50 => {unit => 'm', desc => 'position error' },
pos95 => {unit => 'm', desc => '95% position error' }
};
return $desc;
}
sub _makeVel
{
my $desc= {
vel50 => {unit => 'm/s', desc => 'velocity error' },
vel95 => {unit => 'm/s', desc => '95% velocity error' }
};
return $desc;
}
my $descs = {_makePos(), _makeVel()};
use Data::Dumper;
print Dumper($descs);
this prints only the hash returned from _makeVel. how does it work?
$VAR1 = {
'HASH(0x21ea4a0)' => {
'vel50' => {
'desc' => 'velocity error',
'unit' => 'm/s'
},
'vel95' => {
'unit' => 'm/s',
'desc' => '95% velocity error'
}
}
};
changing this line as
my $descs = {%{_makePos()}, %{_makeVel()}};
worked!
Actually, your original solution did print both of the hashes, but the first one was "stringified" as it was being used as the key of your hash. It's there as HASH(0x21ea4a0).
I see you have a solution, but it might be worth explaining what was going wrong and why your solution fixed it.
Your two subroutines don't return hashes but, rather, hash references. A hash reference is a scalar value that is, effectively, a pointer to a hash.
A hash is created from a list of values. Your code that creates your news hash (actually, once again, a hash reference) is this:
my $descs = {_makePos(), _makeVel()};
This is two scalar values. The first is used as a key in the new hash and the second is used as the associated value - hence the results you get from Data::Dumper.
What you actually want to do is to "dereference" your hashes and get back to the actual hashes. You can dereference a hash using the syntax %{ ... }, where the ... is any expression returning a hash reference. And that's what you've done in your solution. You dereference the hash references, which gives you a list of key/value pairs. The pairs from the two dereferenced hashes are then joined together in a single list which is used to create your new, combined, hash.
I should point out that there's a danger in this approach. If your two subroutines can ever return references to hashes that contain the same key, then only one version of that repeated key will appear in the combined hash.
In an attempt to help me learn Perl, I built the following data structure, where the inner hash (/DriveA/archive, etc.) is a hash reference:
#The contents of the %properties hash of hashes
#The inner hash is a hash reference to a hash named %attributes
$VAR1 = {
'/DriveA' => {
'/DriveA/archive/' => {
'MaxSize' => '20GB',
'Size' => '19GB',
'Free' => '5'
},
'/DriveA/current/' => {
'MaxSize' => '20GB',
'Size' => '12GB',
'Free' => '40'
}
},
'/DriveB' => {
'/DriveB/archive/' => {
'MaxSize' => '8GB',
'Size' => '6GB',
'Free' => '25'
},
'/DriveB/current/' => {
'MaxSize' => '80GB',
'Size' => '20GB',
'Free' => '75'
}
},
'/DriveC' => {
'/DriveC/' => {
'MaxSize' => '20GB',
'Size' => '10GB',
'Free' => '50'
}
}
}
I created an array to hold the keys for %attributes (aka the values/hash reference in %properties) using:
#list = sort keys %attributes;
I'm trying to iterate over the elements in #list and find the associated key for the outer hash in %properties. So, for example, if /DriveA/archive/ is the next item in the array, I want to find the hash key associated with that value, /DriveA, from %properties, assuming dereference of the inner hash.
I created a reverse hash, which outputs the following...
$VAR1 = {
'HASH(0x2002f244)' => '/DriveB',
'HASH(0x2002f388)' => '/DriveC',
'HASH(0x2002f1e4)' => '/DriveA'
}
...using this code...
foreach my $item (#list) {
my %rhash = reverse %properties; # Reverse the hash of hashes so value is key
print Dumper(\%rhash);
}
Question 1:
Given the above, how would I dereference the hash so I can find $item in the hash reference so I can determine the associated value (not the hash reference value).
If $item = '/DriveA/archive/', I want to capture '/DriveA' in a variable from %properties so it can be returned from a subroutine.
I know the inner hash needs to be dereferenced, I'm just not sure how to do it. I've read through perlref, perldsc, and perllol, but I haven't been able to find the answer.
Thanks.
The easiest thing to do is to just generate the reverse keys directly by traversing the data structure:
my %reverse_keys;
foreach my $outer_key (keys %properties) {
my $inner_hashref = $properties->{$outer_key};
my %reverse_map = map { ($_ => $outer_key) } keys %$inner_hashref;
%reverse_keys = (%reverse_keys, %reverse_map);
}
Ideally, you can even generate %reverse_keys directly at the same time you're stashing data into %properties, so you don't need that extra traversal code above.
To address your actual technical question:
You are starting with a hash reference (basically, a pointer in C vernacular).
Then you assign that hash reference as a key into a hash.
Assigning something as a hash key puts it into a string context (a special case of scalar context).
When you stringify a hash reference in Perl, it gets turned into a string representation of that reference - that's your HASH(0x2002f244) string you see when you Data::Dumper your reverse hash.
What you're asking is, on a technical level, is "how do I convert the string representation of a hash reference back into the hash reference itself?"
This is covered in Perl FAQ 4. As far as I'm aware, you can NOT easily convert the string representation of a hash reference back into the hash reference itself.
If you absolutely must (which I strongly recommend against - instead, use the solution at the top of the answer) - you can do so using Devel::Pointer CPAN module.
A solution using that module is shown in this PerlMonks thread.
An additional solution may be to use Tie::RefHash module to use actual hash references instead of their string representations as keys. This is documented in Chapter 8.5.1. "References Don't Work as Hash Keys" of O'Reilly's "Programming Perl, 3rd edition". I would recommend against that bit of madness as well.
$hash = { 'Man' => 'Bill',
'Woman' => 'Mary,
'Dog' => 'Ben'
};
What exactly do Perl's “anonymous hashes” do?
It is a reference to a hash that can be stored in a scalar variable. It is exactly like a regular hash, except that the curly brackets {...} creates a reference to a hash.
Note the usage of different parentheses in these examples:
%hash = ( foo => "bar" ); # regular hash
$hash = { foo => "bar" }; # reference to anonymous (unnamed) hash
$href = \%hash; # reference to named hash %hash
This is useful to be able to do, if you for example want to pass a hash as an argument to a subroutine:
foo(\%hash, $arg1, $arg2);
sub foo {
my ($hash, #args) = #_;
...
}
And it is a way to create a multilevel hash:
my %hash = ( foo => { bar => "baz" } ); # $hash{foo}{bar} is now "baz"
You use an anonymous hash when you need reference to a hash and a named hash is inconvenient or unnecessary. For instance, if you wanted to pass a hash to a subroutine, you could write
my %hash = (a => 1, b => 2);
mysub(\%hash);
but if there is no need to access the hash through its name %hash you could equivalently write
mysub( {a => 1, b => 2} );
This comes in handy wherever you need a reference to a hash, and particularly when you are building nested data structures. Instead of
my %person1 = ( age => 34, position => 'captain' );
my %person2 = ( age => 28, position => 'boatswain' );
my %person3 = ( age => 18, position => 'cabin boy' );
my %crew = (
bill => \%person1,
ben => \%person2,
weed => \%person3,
);
you can write just
my %crew = (
bill => { age => 34, position => 'captain' },
ben => { age => 28, position => 'boatswain' },
weed => { age => 18, position => 'cabin boy' },
);
and to add a member,
$crew{jess} = { age => 4, position => "ship's cat" };
is a lot neater than
my %newperson = ( age => 4, position => "ship's cat" );
$crew{jess} = \%newperson;
and of course, even if a hash is created with a name, if its reference is passed elsewhere then there may be no way of using that original name, so it must be treated as anonymous. For instance in
my $crew_member = $crew{bill}
$crew_member is now effectively a reference to an anonymous hash, regardless of how the data was originally constructed. Even if the data is (in some scope) still accessible as %person1 there is no general way of knowing that, and the data can be accessed only by its reference.
It's quite simple. They allow you to write
push #hashes, { ... };
f(config => { ... });
instead of
my %hash = ( ... );
push #hashes, \%hash;
my %config = ( ... );
f(config => \%config);
(If you want to know the purpose of references, that's another story entirely.)
Anything "anonymous" is a data structure that used in a way where it does not get a name.
Your question has confused everyone else on this page, because your example shows you giving a name to the hash you created, thus it is no longer anonymous.
For example - if you have a subroutine and you want to return a hash, you could write this code:-
return {'hello'=>123};
since it has no name there - it is anonymous. Read on to unwind the extra confusion other people have added on this page by introducing references, which are not the same thing.
This is another anonymous hash (an empty one):
{}
This is an anonymous hash with something in it:
{'foo'=>123}
This is an anonymous (empty) array:
[]
This is an anonymous array with something in it:
['foo',123]
Most of the time when people use these things, they are really trying to magically put them inside of other data structures, without the bother of giving them a waste-of-time temporary name when they do this.
For example - you might want to have a hash in the middle of an array!
#array=(1,2,{foo=>3});
that array has 3 elements - the last element is a hash! ($array[2]->{foo} is 3)
perl -e '#array=(1,2,{foo=>1});use Data::Dumper;print Data::Dumper->Dump([\#array],["\#array"]);'
$#array = [
1,
2,
{
'foo' => 1
}
];
Sometimes you want to don't want to pass around an entire data structure, instead, you just want to use a pointer or reference to the data structure. In perl, you can do this by adding a "\" in front of a variable;
%hashcopy=%myhash; # this duplicates the hash
$myhash{test}=2; # does not affect %hashcopy
$hashpointer=\%myhash; # this gives us a different way to access the same hash
$hashpointer->{test}=2;# changes %myhash
$$hashpointer{test}=2; # identical to above (notice double $$)
If you're crazy, you can even have references to anonymous hashes:
perl -e 'print [],\[],{},\{}'
ARRAY(0x10eed48)REF(0x110b7a8)HASH(0x10eee38)REF(0x110b808)
and sometimes perl is clever enough to know you really meant reference, even when you didn't specifically say so, like my first "return" example:
perl -e 'sub tst{ return {foo=>bar}; }; $var=&tst();use Data::Dumper;print Data::Dumper->Dump([\$var],["\$var"]);'
$var = \{
'foo' => 'bar'
};
or:-
perl -e 'sub tst{ return {foo=>bar}; }; $var=&tst(); print "$$var{foo}\n$var->{foo}\n"'
bar
bar
I want to convert an array of hashes that I create like this:
while(...)
{
...
push(#ranks, {id => $id, time => $time});
}
To JSON:
use JSON;
$j = new JSON;
print $j->encode_json({ranks => #ranks});
But it is outputting this:
{"ranks":{"time":"3","id":"tiago"},
"HASH(0x905bf70)":{"time":"10","id":"bla"}}
As you can see it isnt able to write on of the hashes and there's no array...
I would like to output a JSON string that looked like this:
{"ranks":[{"time":"3","id":"tiago"},
{"time":"40","id":"fhddhf"},
{"time":"10","id":"bla"}]}
All of these are the same:
ranks => #ranks
'ranks', #ranks
'ranks', $ranks[0], $ranks[1], $ranks[2]
ranks => $ranks[0], $ranks[1] => $ranks[2]
So you're creating a hash with two elements when you mean to create a hash with one element.
You tried to use an array as a hash value, but hash values can only be scalars. It is common, however, to use a reference to an array as a hash value since references are scalars, and this is what encode_json expects.
print $j->encode_json( { ranks => #ranks } );
should be
print $j->encode_json( { ranks => \#ranks } );
print $j->encode_json({ranks => #ranks});
should be:
print $j->encode_json({ranks => \#ranks});
Try passing the array as a reference.
to_json({ranks => \#ranks},{ascii => 1,pretty => 1});