Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think I would build a table of regular expressions from the ini file and then simply compare the strings one by one.</p> <p>My suggested code is below. The <code>get_correct_key</code> now converts all underscores to hyphens and trips out all the junk so, for example, <code>SOUTH-GWS-F-IDL100555236-Primary-1Gb</code> is converted to <code>SOUTH-GWS-F-IDL-Primary-Gb</code>. That has allowed me to simplify the regex substantially too.</p> <p>Some notes on your code</p> <ul> <li><p>It is superior to <code>use warnings</code> instead of adding <code>-w</code> to the shebang line</p></li> <li><p>Capital letters in identifiers are generally reserved for globals like packages, and anyone familiar with Perl would thank you to stick to lower case and underscores for simple variables and subroutines</p></li> <li><p><code>$@</code> is only set when a piece of Perl code fails to compile or calls <code>die</code>. It is only caught by <code>eval</code>, and if you aren't using that then your tests on <code>$@</code> will always fail</p></li> <li><p>You should <em>never</em> use prototypes on a Perl subroutine definition. They are different from prototypes in other languages and don't do what you think</p></li> </ul> <p>I hope this helps</p> <pre><code>use strict; use warnings; open my $ini, '&lt;', 'user_defined_connection.ini' or die $!; my @provider_patterns; while (&lt;$ini&gt;) { next unless /\S/; chomp; my ($key, $data) = split /=/, $_, 2; my $regex = join " |\n", map { join ' ', map { "(?=.* \\b $_ \\b )" } split /,/; } split /\|\|/, $data; push @provider_patterns, [ qr/$regex/xi, $key ]; } my @aliases = ( 'AFGHD_NORTH', 'NORTHERN_IIDID_IPV123', 'IDL_SOUTH', 'IDL_SOUTH_IUID', 'SOUTHERN_IND_IPV', 'IDL_NORTH_IPV', 'IDL_ABDGJF', 'IDL SOUTH', 'MANAGEMENT_IPV_IDL100595208', 'SOUTH-GWS-F-IDL100555236-Primary-1Gb', ); for my $alias (@aliases) { my $found = get_correct_key($alias, \@provider_patterns); printf qq{ %-38s %-s\n}, qq{"$alias"}, defined $found ? $found : "Doesn't match anything so return undef"; } sub get_correct_key { my ($alias, $patterns) = @_; $alias =~ tr/_/-/; $alias = join '-', $alias =~ /[a-z]+/gi; my $found; for my $pair (@$patterns) { my ($re, $key) = @$pair; $found = $key if $alias =~ $re; } $found; } </code></pre> <p><strong>output</strong></p> <pre><code> "AFGHD_NORTH" Doesn't match anything so return undef "NORTHERN_IIDID_IPV123" Doesn't match anything so return undef "IDL_SOUTH" SOUTH_IPV "IDL_SOUTH_IUID" SOUTH_IPV "SOUTHERN_IND_IPV" Doesn't match anything so return undef "IDL_NORTH_IPV" NORTH_IPV "IDL_ABDGJF" MANAGEMENT "IDL SOUTH" SOUTH_IPV "MANAGEMENT_IPV_IDL100595208" MANAGEMENT "SOUTH-GWS-F-IDL100555236-Primary-1Gb" SOUTH_IPV </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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