learnP has asked for the wisdom of the Perl Monks concerning the following question:
I have the below requirements * Read the sample data file /tmp/user_defined_connection.ini
MANAGEMENT=IDL||CIDL NORTH=IDL,NORTHERN||VIDL,NORTH||IDL,NORTH SOUTH=IDL,SOUTHERN||CIDL,SOUTH||IDL,SOUTH
Each Key here can have the multiple such values ',' signifies an AND operations and '||' is an OR operation * 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 .... `$instance` contains `IDL` or `CIDL` In next iteration same instance will be checked against the second key which is `NORTH` 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
How the check should work in this example 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) So we have to check against all values in hash %idlcodes and return the last key with which match was TRUE The sequence is managed by the array `@providerCloudSequence` 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)
#!/sbin/perl -w use strict; use Data::Dumper; my @providerCloudSequence; my %idlcodes; open(my $iniFH, '<', '/tmp/user_defined_connection.ini') or die "Unable to open file:$!\n"; while(<$iniFH>) { chomp; my @providerCloudClass = split('=',$_); push @providerCloudSequence, $providerCloudClass[0]; if ($@) { print "Script Failed to get the Sequence....Exiting the Sc +ript\n"; exit 1; } #my %idlcodes = map { split /=|\s+/; } <$iniFH>; my ($k, @r) = split /=|\Q||/; $idlcodes{$k} = [ map [ split /,/ ], @r ]; } if($@){ print "Script Failed to create the hash....Exiting the Scr +ipt\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, @provide +rCloudSequence) print $correctKey; } } # Providercloudsequence helps to maintain the precedence of the in +put file # idlcodes is the hasp map where key is the key from file and valu +e 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 wa +nt to # grep $interfaceAlias against each with a AND in between elem +ents # and OR in between arrays , if true this is match } } ##Data Dumper Output : Hash looks like 'NORTH_IPV' => [ [ 'IDL', 'NORTHERN' ], [ 'VIDL', 'NORTH' ], [ 'IDL', 'NORTH' ] ], 'MANAGEMENT' => [ [ 'IDL' ], [ 'CIDL' ] ], 'SOUTH_IPV' => [ [ 'IDL', 'SOUTHERN' ], [ 'CIDL', 'SOUTH' ], [ 'IDL', 'SOUTH' ] ] <p><b> #### Expected output 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 </b></p>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: checking the string against all values of multidimension hashmap in perl
by Kenosis (Priest) on Nov 20, 2013 at 19:39 UTC |