Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: Perl Module for identifying country name

by TomDLux (Vicar)
on Aug 03, 2012 at 16:09 UTC ( [id://985272] : note . print w/replies, xml ) Need Help??

in reply to Perl Module for identifying country name

You can search for any group of strings you wish to. The problem is, what are the possible values. Will it be the English name or the German on: Germany or Deutchland? Will it be the current name or an older one: Myamar or Burma? Sri Lanka or Ceylon? Mumbai or Bombay?

If you have a file with one value per line, you can use "grep -f countries datafile" to examine datafile for all the countries in the countries file. The perl equivalent is simple:

  • read in the set of countries into an array
  • form into a regular expression which will capture the found string:
    my $re_text = join '|', map {($_)} @countries; my $re = rx/$re_text/;
  • and then test each input line against the re:
    while ( my $line = <$fh>) { chomp $line; my $found = ($line =~ /$re/); # Profit! }

As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Replies are listed 'Best First'.
Re^2: Perl Module for identifying country name
by CountZero (Bishop) on Aug 03, 2012 at 17:20 UTC
    What use is the map {($_)} in your regex-making code? You will create a great many "captures" which are unnecessary.


    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics