Re: Dummy regex question
From: Tad McClellan (tadmc_at_augustmail.com)
Date: 01/06/05
- Next message: Tad McClellan: "Re: Having Trouble with a CGI login script"
- Previous message: Chris Mattern: "Re: Extra new line inserted in simple program"
- In reply to: Sven-Thorsten Fahrbach: "Re: Dummy regex question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 5 Jan 2005 18:21:24 -0600
Sven-Thorsten Fahrbach <sven-thorsten.fahrbach@gmx.net> wrote:
> JayEs wrote:
>
>
>> Given a string that can be:
>> "$ 12.00"
>> "USD 187.54"
>> "CAD 1.20"
>> "??? 12.65"
>>
>> (the latter due to special characters not avail in ASCII)
>>
>> How do I split these strings into 2, separating the currency code and the
>> value?
>
> I don't get why everyone tries to use split() here.
Because it is the right tool for the job.
You use split() when you know what you want to discard.
You use m// in a list context when you know what you want to keep.
> I'd take a simple
> regexp looking like
>
> m/(.+)\s+(\d+\.\d+)/
> my ($currency, $amount) = ($1, $2);
You should never use the dollar-digit variables unless you have
first ensured that the pattern match *succeeded*.
if ( m/(.+)\s+(\d+\.\d+)/ )
{ ($currency, $amount) = ($1, $2) }
or
my ($currency, $amount) = m/(.+)\s+(\d+\.\d+)/; # m// in list context
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
- Next message: Tad McClellan: "Re: Having Trouble with a CGI login script"
- Previous message: Chris Mattern: "Re: Extra new line inserted in simple program"
- In reply to: Sven-Thorsten Fahrbach: "Re: Dummy regex question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|