leofer
BAN USER##################################################
# Function IsArray #
# Input: reference #
# Output: 1 iff input is a reference to an array #
##################################################
sub IsArray{
if(ref($_[0]) eq 'ARRAY'){
1;
}else{
0;
}
}
##################################################
# Function IsHash #
# Input: reference #
# Output: 1 iff input is a reference to a Hash #
##################################################
sub IsHash{
#print "DEBUG-------->",ref($_[0]),"\n";
if(ref($_[0]) eq 'HASH'){
1;
}else{
0;
}
}
##################################################
# Function Search #
# Input: nested hash , key #
# Output: If key exists in the hash at any level,#
# output the value, otherwise undef #
# Algorithm: Recursively go on the values of the #
# input hash / array. #
##################################################
sub Search{
my ($hashRef , $key ) = @_;
# print "DEBUG2 ----> Seaching for $key\n";
# Input is a hash - check for the key, and if not go through the other values
if (IsHash($hashRef)) {
if (exists($$hashRef{$key})) {
return ($$hashRef{$key});
}
# Recursively go through the values of the hash
for (values %$hashRef) {
if (IsArray($_) or IsHash($_)) {
my $temp = Search($_,$key);
if (defined $temp) {
return $temp
}
}
}
}
if (IsArray($hashRef)) {
# Recursively go through the values of the array
for (@$hashRef) {
if (IsArray($_) or IsHash($_)) {
my $temp = Search($_,$key);
if (defined $temp) {
return $temp
}
}
}
}
return undef;
}
- leofer October 22, 2012