note
thanos1983
<p>Hello [id://1232891|Newbie95],</p>
<p>Just a minor note here because you said you are doing your first steps on hashes.</p>
<p>I noticed that you have an array of keys and a hash below. If the hash is not manually defined you can use hash slicing and assign the keys and values in one step to avoid typing everything manually. For example see below:</p>
<code>
my @keys = qw(b d a c);
my @values = qw(200 100 30 40);
my %hash;
@hash{@keys} = @values;
print Dumper \%hash;
</code>
<p>Then simply as fellow Monks demonstrate to you, iterate over the keys and if the key exists print.</p>
<code>
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my @keys = qw(b d a c);
my @values = qw(200 100 30 40);
my %hash;
@hash{@keys} = @values;
print Dumper \%hash;
foreach my $key (@keys) {
if (exists $hash{$key} ) {
print "array element: $key = $hash{$key} inside \%hash\n";
}
next;
}
__END__
$ perl test.pl
$VAR1 = {
'a' => '30',
'c' => '40',
'b' => '200',
'd' => '100'
};
array element: b = 200 inside %hash
array element: d = 100 inside %hash
array element: a = 30 inside %hash
array element: c = 40 inside %hash
</code>
<p>Here is the documentation regarding [https://perldoc.perl.org/perldata.html#Slices|perldata/Slices]. In case you want to read a bit more on how to play with them :).</p>
<p>BR / Thanos</p>
<div class="pmsig"><div class="pmsig-1069339">
Seeking for Perl wisdom...on the process of learning...not there...yet!
</div></div>
11100018
11100018