×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# Find if Hash Value exists in Hash of Arrays

## Find if Hash Value exists in Hash of Arrays

(OP)
Hi there,

If I have a Hash of Arrays, how to you find out if a value exists for a given key.

#### CODE

use Data::Dumper;

#populate hash
foreach (1 .. 4) {
push @{ $hash{key1} },$_;
}

foreach (5 .. 9) {
push @{ $hash{key2} },$_;
} 
So if I wanted to find out if '1' exists in key1, how do I do that?

I've tried, if exists, grep, defined, searched this forum, can't get it to work etc

### RE: Find if Hash Value exists in Hash of Arrays

(OP)
Thanks whn, I didn't make my question very clear in the first place. I should have said, if the value exists, then do nothing, if not adding the value to that key.

#### CODE

$VAR1 = { 'key2' => [ 5, 6, 7, 8, 9 ], 'key1' => [ 1, 2, 3, 4 ] };  So in the scenario above, key1 already has the value of '1', so no need to add. But if$var1 was 10, then it would need to be added.

My attempt at this has not been too successful

#### CODE

foreach (1 .. 4) {
push @{ $hash{key1} },$_;
}

foreach (5 .. 9) {
push @{ $hash{key2} },$_;
}

$var1 = "key1";$var2 = 1;
foreach $key (keys(%hash)) { for($i = 0; $i <=$#{$hash{$key}}; $i++) { if($var2 == $hash{$key}[$i]) { print "Found$var1 doing nothing\n";
print "Found '$var1'! Key:$key, \$i =$i\n";
}
else{
push @{ $hash{$var1} },  $var2; print "Adding$var2 to $var1\n"; } } }  ### RE: Find if Hash Value exists in Hash of Arrays For example: stevio.pl #### CODE use strict; #use warnings; my$key = "";
my %lst_hash = ();

# populate hash
# key1
$key = 'key1'; my @lst = (1 .. 4); # add reference of @lst$lst_hash{$key} = \@lst;$key = 'key2';
my @lst = (5 .. 8);
$lst_hash{$key} =  \@lst;

# print hash
print "Hash contains elements:\n";
print_hash();

$key = 'key1'; my$element = 3;
print "Trying to add element='$element' for key='$key':\n";
value2hash($key,$element);
print "Hash contains elements:\n";
print_hash();

$key = 'key2';$element = 9;
print "Trying to add element='$element' for key='$key':\n";
value2hash($key,$element);
print "Hash contains elements:\n";
print_hash();

sub print_hash {
# print hash
foreach $key (sort keys(%lst_hash)){ # dereference list my @tmp_lst = @{$lst_hash{$key}}; print "'$key' => $lst_hash{$key} = (@tmp_lst)\n";
}
print "\n";
}

sub value2hash {
# add value to hash if it doesn't exist
my ($key,$val) = @_;
# dereference list
my @lst = @{$lst_hash{$key}};
my $val_exist = 0; foreach my$j (@lst) {
if ($j ==$val) {
$val_exist = 1; last; } } if ($val_exist == 0) {
push (@lst, $val) } else { print "ERROR: For key = '$key' value $val already exists !\n"; } # add reference of @lst$lst_hash{$key} = \@lst; }  Output: #### CODE C:\_mikrom\Work>perl stevio.pl Hash contains elements: 'key1' => ARRAY(0x29a28c) = (1 2 3 4) 'key2' => ARRAY(0x182c1ac) = (5 6 7 8) Trying to add element='3' for key='key1': ERROR: For key = 'key1' value 3 already exists ! Hash contains elements: 'key1' => ARRAY(0x183c654) = (1 2 3 4) 'key2' => ARRAY(0x182c1ac) = (5 6 7 8) Trying to add element='9' for key='key2': Hash contains elements: 'key1' => ARRAY(0x183c654) = (1 2 3 4) 'key2' => ARRAY(0x182bd9c) = (5 6 7 8 9)  ### RE: Find if Hash Value exists in Hash of Arrays #### CODE #!/usr/bin/perl use strict; use warnings FATAL => qw/ all /; use List::MoreUtils qw/ any /; use Data::Dumper; # construct hash my %hash; push @{$hash{key1}}, $_ for ( 1 .. 4 ); push @{$hash{key2}}, $_ for ( 5 .. 9 ); # process hash my$var1 = 1;
while ( my ( $key,$list ) = ( each %hash ) )
{
push @$list,$var1 unless ( any { $_ ==$var1 } @\$list );
}

# print hash
print Dumper \%hash; 

Chris

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!