Beefy Boxes and Bandwidth Generously Provided by pair Networks kudra
The stupid question is the question not asked
 
PerlMonks  

Is there a better way to get the Country names with help of Regax

by Anonymous Monk
on Oct 16, 2012 at 04:44 UTC ( #999217=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

$names = Location:G-Canada

$names = Location:Germany

$names = Location:p-Australia

$names = Location:e-Britain

$names = Location:USA

$names = Location:c-India

$names = Location:Netherlands

$names = Location:r-China

my ($location) = $names =~ /Location:(.*)/i; $location= $location=~ /-(.*)/i;

Currently I am doing this to get the Country names. (See some are having c- or r- or something in front of the names). My question is, Is there a better way to do it(in a single code). Thanks in advance Monks.

Comment on Is there a better way to get the Country names with help of Regax
Download Code
Re: Is there a better way to get the Country names with help of Regax
by Tux (Monsignor) on Oct 16, 2012 at 05:39 UTC
    my ($location) = m{\b Location: (?:[a-zA-Z]-)? (\S+) }x;

    Enjoy, Have FUN! H.Merijn

      It would be good to see more sample input to figure out how countries with spaces in their names should be dealt with. North Korea, Vatican City, South Sudan, Timor Leste, Papua New Guinea, etc...

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

        Thanks Toby...

      Thanks Tux. :-)

Re: Is there a better way to get the Country names with help of Regax
by 2teez (Priest) on Oct 16, 2012 at 09:23 UTC

    Hi, You could use this

    print $1, $/ if ( $location =~ m{(?<=:)(?:.-)?(.+?)$} );
    even addressing the excellent point raised by tobyink
    like so:
    #!/usr/bin/perl use warnings; use strict; while ( defined( my $location = <DATA> ) ) { chomp $location; print $1, $/ if ( $location =~ m{(?<=:)(?:.-)?(.+?)$} ); } __DATA__ $names = Location:G-Canada $names = Location:Germany $names = Location:p-Australia $names = Location:e-Britain $names = Location:USA $names = Location:c-India $names = Location:Netherlands $names = Location:r-China $names = Location:North Korea $names = Location:Vatican City $names = Location:South Sudan $names = Location:Timor Leste $names = Location:Papua New Guinea
    OUTPUT
    Canada Germany Australia Britain USA India Netherlands China North Korea Vatican City South Sudan Timor Leste Papua New Guinea

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://999217]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2014-04-20 03:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls