PERL – Mapping An Element To Its Array

Disclaimer

Often times, a need arises where one has to search a bunch of arrays to find out to which one of them a given element belongs. Of course, life becomes a lot simpler if those arrays are unique (meaning any given element appears only in one of the arrays) and life may not always be that simple.

Some common examples of such simple case include, but not limited to, the following:

  1. Decide which fruit belongs to which basket
  2. Decide which database to query based on a set criterion. Certainly helps if the array names are the same as respective database names. There are potentially very many such database driven applications
  3. And so on.

The Script/Module

Save the following into a file and call it, element_array_map.pl (or element_array_map.pm, as the need may be). It provides a function/sub routine that takes the element name as the input and returns the array name to which the requested element belongs as output. The sub-routine makes a hash of all the arrays and uses foreach loop to sift through them. The array name (depending on the application), can subsequently used appropriately. (Re)naming the variables and functions to describe the specific application may also be useful.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#! /usr/bin/perl -wT
 
#
# PERL module/script to decide to which array an element belongs to
# when there are multiple arrays.
# Assumes that the arrays contain UNIQUE element names
# Element_Array_Map() takes in elementName as input and returns the name of array it's in.
# 
 
#
# sub Element_Array_Map() BEGINS
 
sub Element_Array_Map($) {
 
#
# Requested elementName
 
  local ($element) = $_[0];
 
#
# Uncomment the following line for debugging purposes
# print("Requested element is $element\n");
 
#
# A hash of arrays
# Each array contains several element names
# Each array can have different number of elements 
# Elements of each array are UNIQUE
# Hash definition can be modified to include 'N' arrays
 
  my %HASH_OF_ARRAYS = (
      ARRAY1 => ['Element1', 'Element2'], 
      ARRAY2 => ['Element3', 'Element4', 'Element5', 'Element6'],
      ARRAY3 => ['Element7', 'Element8', 'Element9'],
      ARRAY4 => ['Element0'],
      );
 
#
# Search the hash of arrays to find out to which array does the 
# requested element belong
 
  foreach my $element_array (keys %HASH_OF_ARRAYS) {
    foreach (@{$HASH_OF_ARRAYS{$element_array}}) {
      if ( $element =~ m/$_/ ) {  
        print "$element is in $element_array\n";
        return $element_array;
      }
    }
  }
 
} 
# sub Element_Array_Map() ENDS
 
#
# Call it after the function to avoid the following error
# 'main::Element_Array_Map() called too early to check prototype'
# If not using '-wT' flags, this function can be called before its 
# definition. The value '$element_array' can be passed on to other 
# modules/scripts, if necessary.
 
my $elementName   = "Element1";
my $element_array = Element_Array_Map($elementName);
 
# Uncomment the following line for debugging purposes
# print "$element_array\n";


Mapping An Element To Its Array

One Reply to “PERL – Mapping An Element To Its Array”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.