Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find if Hash Value exists in Hash of Arrays

Status
Not open for further replies.

stevio

Vendor
Jul 24, 2002
78
AU
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
Code:
$var1 = 1;
my %reverse = reverse %hash;
if( defined( $reverse{$var1} ) ) {
  print "1 is a value in the hash!\n";
}
else {
 print "1 is not a value in the hash!\n";
}

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

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

$var1 = 1;
foreach $key (keys(%hash)) {
  for($i = 0; $i <= $#{$hash{$key}}; $i++) {
    if($var1 == $hash{$key}[$i]) {
      print "Found '$var1'! Key: $key, \$i = $i\n";
    }
  }
}

Output:
Code:
Found '1'! Key: key1, $i = 0
 
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";
	}
  }
}

 
For example:
stevio.pl
Code:
[COLOR=#804040][b]use strict[/b][/color];
[COLOR=#0000ff]#use warnings;[/color]

[COLOR=#804040][b]my[/b][/color] [COLOR=#008080]$key[/color] = [COLOR=#ff00ff]""[/color];
[COLOR=#804040][b]my[/b][/color] [COLOR=#008080]%lst_hash[/color] = ();

[COLOR=#0000ff]# populate hash[/color]
[COLOR=#0000ff]# key1[/color]
[COLOR=#008080]$key[/color] = [COLOR=#ff00ff]'[/color][COLOR=#ff00ff]key1[/color][COLOR=#ff00ff]'[/color];
[COLOR=#804040][b]my[/b][/color] [COLOR=#008080]@lst[/color] = ([COLOR=#ff00ff]1[/color] .. [COLOR=#ff00ff]4[/color]);
[COLOR=#0000ff]# add reference of @lst[/color]
[COLOR=#008080]$lst_hash{$key}[/color] =  \[COLOR=#008080]@lst[/color];

[COLOR=#008080]$key[/color] = [COLOR=#ff00ff]'[/color][COLOR=#ff00ff]key2[/color][COLOR=#ff00ff]'[/color];
[COLOR=#804040][b]my[/b][/color] [COLOR=#008080]@lst[/color] = ([COLOR=#ff00ff]5[/color] .. [COLOR=#ff00ff]8[/color]);
[COLOR=#0000ff]# add reference of @lst[/color]
[COLOR=#008080]$lst_hash{$key}[/color] =  \[COLOR=#008080]@lst[/color];

[COLOR=#0000ff]# print hash[/color]
[COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#ff00ff]Hash contains elements:[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];
print_hash();

[COLOR=#008080]$key[/color] = [COLOR=#ff00ff]'[/color][COLOR=#ff00ff]key1[/color][COLOR=#ff00ff]'[/color];
[COLOR=#804040][b]my[/b][/color] [COLOR=#008080]$element[/color] = [COLOR=#ff00ff]3[/color];
[COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#ff00ff]Trying to add element='[/color][COLOR=#008080]$element[/color][COLOR=#ff00ff]' for key='[/color][COLOR=#008080]$key[/color][COLOR=#ff00ff]':[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];
value2hash([COLOR=#008080]$key[/color], [COLOR=#008080]$element[/color]);
[COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#ff00ff]Hash contains elements:[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];
print_hash();

[COLOR=#008080]$key[/color] = [COLOR=#ff00ff]'[/color][COLOR=#ff00ff]key2[/color][COLOR=#ff00ff]'[/color];
[COLOR=#008080]$element[/color] = [COLOR=#ff00ff]9[/color];
[COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#ff00ff]Trying to add element='[/color][COLOR=#008080]$element[/color][COLOR=#ff00ff]' for key='[/color][COLOR=#008080]$key[/color][COLOR=#ff00ff]':[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];
value2hash([COLOR=#008080]$key[/color], [COLOR=#008080]$element[/color]);
[COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#ff00ff]Hash contains elements:[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];
print_hash();


[COLOR=#804040][b]sub [/b][/color][COLOR=#008080]print_hash [/color]{
  [COLOR=#0000ff]# print hash[/color]
  [COLOR=#804040][b]foreach[/b][/color] [COLOR=#008080]$key[/color] ([COLOR=#804040][b]sort[/b][/color] [COLOR=#804040][b]keys[/b][/color]([COLOR=#008080]%lst_hash[/color])){
    [COLOR=#0000ff]# dereference list[/color]
    [COLOR=#804040][b]my[/b][/color] [COLOR=#008080]@tmp_lst[/color] = [COLOR=#008080]@{$lst_hash{$key}}[/color];
    [COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#ff00ff]'[/color][COLOR=#008080]$key[/color][COLOR=#ff00ff]' => [/color][COLOR=#008080]$lst_hash{$key}[/color][COLOR=#ff00ff] = ([/color][COLOR=#008080]@tmp_lst[/color][COLOR=#ff00ff])[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];
  }
  [COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];
}

[COLOR=#804040][b]sub [/b][/color][COLOR=#008080]value2hash [/color]{
  [COLOR=#0000ff]# add value to hash if it doesn't exist[/color]
  [COLOR=#804040][b]my[/b][/color] ([COLOR=#008080]$key[/color], [COLOR=#008080]$val[/color]) = [COLOR=#008080]@_[/color];
  [COLOR=#0000ff]# dereference list[/color]
  [COLOR=#804040][b]my[/b][/color] [COLOR=#008080]@lst[/color] = [COLOR=#008080]@{$lst_hash{$key}}[/color];
  [COLOR=#804040][b]my[/b][/color] [COLOR=#008080]$val_exist[/color] = [COLOR=#ff00ff]0[/color];
  [COLOR=#804040][b]foreach[/b][/color] [COLOR=#804040][b]my[/b][/color] [COLOR=#008080]$j[/color] ([COLOR=#008080]@lst[/color]) {
     [COLOR=#804040][b]if[/b][/color] ([COLOR=#008080]$j[/color] == [COLOR=#008080]$val[/color]) { 
      [COLOR=#008080]$val_exist[/color] = [COLOR=#ff00ff]1[/color];
      [COLOR=#804040][b]last[/b][/color];
    }
  }

  [COLOR=#804040][b]if[/b][/color] ([COLOR=#008080]$val_exist[/color] == [COLOR=#ff00ff]0[/color]) {
     [COLOR=#804040][b]push[/b][/color] ([COLOR=#008080]@lst[/color], [COLOR=#008080]$val[/color]) 
  }
  [COLOR=#804040][b]else[/b][/color] {
    [COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#ff00ff]ERROR: For key = '[/color][COLOR=#008080]$key[/color][COLOR=#ff00ff]' value [/color][COLOR=#008080]$val[/color][COLOR=#ff00ff] already exists ![/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];
  }
  [COLOR=#0000ff]# add reference of @lst[/color]
  [COLOR=#008080]$lst_hash{$key}[/color] =  \[COLOR=#008080]@lst[/color];
}

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)

 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top