Note that there are some explanatory texts on larger screens.

plurals
  1. POchecking the string against all values of multidimension hashmap in perl
    text
    copied!<p>I have the below requirements</p> <ul> <li>Read the sample data file /tmp/user_defined_connection.ini</li> </ul> <hr> <pre><code>MANAGEMENT=IDL||CIDL NORTH=IDL,NORTHERN||VIDL,NORTH||IDL,NORTH SOUTH=IDL,SOUTHERN||CIDL,SOUTH||IDL,SOUTH </code></pre> <ul> <li><p>Each Key here can have the multiple such values ',' signifies an AND operations and '||' is an OR operation</p></li> <li><p>I need to check this against another string $instance and for each value , so I take the first key MANAGEMENT and get its values and check against isntance ....</p></li> </ul> <p><code>$instance</code> contains <code>IDL</code> or <code>CIDL</code></p> <p>In next iteration same instance will be checked against the second key which is <code>NORTH</code> and if true the function should return NORTH so basically I need to check the instance against each value one by one and then return the key which i find the last... below is the code written till now</p> <pre><code>#!/sbin/perl -w use strict; use Data::Dumper; my @providerCloudSequence; my %idlcodes; open(my $iniFH, '&lt;', '/tmp/user_defined_connection.ini') or die "Unable to open file:$!\n"; while(&lt;$iniFH&gt;) { chomp; my @providerCloudClass = split('=',$_); push @providerCloudSequence, $providerCloudClass[0]; if ($@) { print "Script Failed to get the Sequence....Exiting the Script\n"; exit 1; } #my %idlcodes = map { split /=|\s+/; } &lt;$iniFH&gt;; my ($k, @r) = split /=|\Q||/; $idlcodes{$k} = [ map [ split /,/ ], @r ]; } if($@){ print "Script Failed to create the hash....Exiting the Script\n"; exit 1; } close $iniFH; print Dumper \%idlcodes; my @interfaceSampleAliases = { "AFGHD_NORTH", "NORTHERN_IIDID_IPV123", "IDL_SOUTH", "IDL_SOUTH_IUID", "SOUTHERN_IND_IPV" }; foreach (@interfaceSampleAliases){ my $correctKey = getCorrectKey($_, %idlcodes, @providerCloudSequence) print $correctKey; } } # Providercloudsequence helps to maintain the precedence of the input file # idlcodes is the hasp map where key is the key from file and value is an arracy of arrays ...see below the output of same sub getCorrectKey($$$){ my $interfaceAlias = $_[0]; foreach ($_[2]) { # This is where I need the help , I want to get the value from # hash %idlcodes using the key from @providerCloudSequence # The value of %idlcodes is kind of multiple Arrays where I want to # grep $interfaceAlias against each with a AND in between elements # and OR in between arrays , if true this is match } } </code></pre> <p>##Data Dumper Output : Hash looks like</p> <pre><code>'NORTH_IPV' =&gt; [ [ 'IDL', 'NORTHERN' ], [ 'VIDL', 'NORTH' ], [ 'IDL', 'NORTH' ] ], 'MANAGEMENT' =&gt; [ [ 'IDL' ], [ 'CIDL' ] ], 'SOUTH_IPV' =&gt; [ [ 'IDL', 'SOUTHERN' ], [ 'CIDL', 'SOUTH' ], [ 'IDL', 'SOUTH' ] ] </code></pre> Expected output <pre><code>InterfaceAlias correctKey "AFGHD_NORTH" Doesnt Match anything so return NULL "NORTHERN_IIDID_IPV123" Doesnt Match anything so return NULL "IDL_SOUTH", SOUTH_IPV "IDL_SOUTH_IUID", SOUTH_IPV "SOUTHERN_IND_IPV" Doesnt Match anything so return NULL "IDL_NORTH_IPV" NORTH_IPV "IDL_ABDGJF" MANAGEMENT </code></pre> <p>How the check should work in this example</p> <p>checks if InterfaceAlias contains (IDL OR CIDL) , then checks if same interfaceAlias contains [(IDL AND NORTH) OR (VIDL AND NORTH) OR (IDL AND NORTHERN) , then checks if same interfaceAlias contains [(IDL AND SOUTH) OR (CIDL AND SOUTH) OR (IDL AND SOUTHERN)]</p> <p>So we have to check against all values in hash %idlcodes and return the last key with which match was TRUE</p> <p>The sequence is managed by the array <code>@providerCloudSequence</code></p> <p>Adjacency of words is not important For example SOUTH_XXX_CIDL or even "SOUTH IDL IPV WITH SPACES" should match SOUTH_IPV....These are just examples it is basically a contains match....though the structure of file is fixed...(the data may vary)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload