use strict; use warnings; my %hash; my @interfaceSampleAliases = ( "AFGHD_NORTH", "NORTHERN_IIDID_IPV123", "IDL_SOUTH", "IDL_SOUTH_IUID", "SOUTHERN_IND_IPV", "IDL_NORTH_IPV", "IDL_ABDGJF" ); chomp( my @iniFileData = ); initInterfaceAliasHash( \%hash, \@iniFileData ); for my $interface (@interfaceSampleAliases) { if ( my $key = findInterfaceAlias( \%hash, \$interface ) ) { print "'$interface' matches key: $key\n"; } else { print "'$interface': No match\n"; } } sub initInterfaceAliasHash { my ( $hashRef, $iniFileDataRef ) = @_; my $i = 1; for (@$iniFileDataRef) { my @ands; my ( $key, $val ) = split /=/; my @ors = split /\|\|/, $val; for my $or (@ors) { push @ands, join '', map { "(?=.*\\b\Q$_\E\\b)" } split /,/, $or; } $$hashRef{ $i++ }{$key} = join '|', map { "(?:$_)" } @ands; } } sub findInterfaceAlias { my ( $hashRef, $interfaceAliasRef ) = @_; my $lastMatch = 0; my $interfaceAlias = $$interfaceAliasRef; $interfaceAlias =~ s/_/ /g; for my $key1 ( sort { $a <=> $b } keys %$hashRef ) { my $key2 = ( keys $$hashRef{$key1} )[0]; $lastMatch = $key1 if $interfaceAlias =~ /$$hashRef{$key1}{$key2}/ and $key1 > $lastMatch; } return ( keys $$hashRef{$lastMatch} )[0] if $lastMatch; } __DATA__ MANAGEMENT=IDL||CIDL NORTH_IPV=IDL,NORTHERN||VIDL,NORTH||IDL,NORTH SOUTH_IPV=IDL,SOUTHERN||CIDL,SOUTH||IDL,SOUTH