PERL – Mapping Elements To Their Respective Arrays

Often times, a need arises where one has to search a bunch of arrays to find out to which one of them a given set of elements belong. 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.

If you are looking for methods to map just one element (in question) to its array, you may refer to my previous write up.

The Script/Module

Save the following into a file and call it, elements_arrays_map.pl (or elements_arrays_map.pm, as the need may be). It provides a function/sub routine that takes the element names as the input and returns the array names to which the requested elements belong as an array. 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
66
67
68
69
70
71
72
#! /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 elementNames as input and returns the 
# name of arrays they are in.
# 
 
#
# sub Elements_Arrays_Map() BEGINS
 
sub Elements_Arrays_Map (@) {
 
#
# Requested elementNames
 
  my @elements = @_;
 
#
# Uncomment the following lines for debugging purposes
# foreach my $element (@{elements}) {
#   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 arrays do the 
# requested elements belong
 
  my $i = 0;
  foreach my $element (@{elements}) {
    foreach my $element_array (keys %HASH_OF_ARRAYS) {
      foreach (@{$HASH_OF_ARRAYS{$element_array}}) {
        if ( $element =~ m/$_/ ) {  
          print "$element is in $element_array\n";
          $output_array[$i] = $element_array;
        }
      }
    }
    $i++;
  }
  return @output_array;
} 
# sub Elements_Arrays_Map() ENDS
 
#
# Call it after the function to avoid the following error
# 'main::Elements_Arrays_Map() called too early to check prototype'
# If not using '-wT' flags, this function can be called before its 
# definition. The value '@output_array' can be passed on to other 
# modules/scripts, if necessary.
 
my @elementNames = ("Element1", "Element0", "Element4");
my @output_array = Elements_Arrays_Map(@elementNames);
 
# Uncomment the following line for debugging purposes
# print "$element_array\n";


Mapping Elements To Their Respective Arrays

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.