Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Re: Is there a better way than this?

Thread Tools

Re: Is there a better way than this?

Jürgen Exner
Posts: n/a
Dave Stratford <(E-Mail Removed)> wrote:
>The Outcode however, has six possible formats, excluding some odd ones
>that I know I won't have to worry about: A9, AA9, A99, AA9, A9A and AA9A.
>My requirements are to extract the initial letter(s) from the outcode. So
>if a user entered HP13, I want to extract just the HP, equally if they
>emtered WC1A, I want just the WC.
>My current code, which works perfectly fine, looks like this:
> my $oc = substr($outcode,0,2);
> my $ocr = substr($oc,1,1);
> $oc = substr($outcode,0,1) if ($ocr =~ /\d/);

Trust the magic of REs (untested):
$outcode =~ /(\D*)/;
$oc = $1;

Or maybe
$outcode =~ /(\w*)/;
$oc = $1;

As long as the outcode is valid both variations should yield the same
result. Only if you have a non-valid code then they will return
different results.

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Who can explain this bug? mathog C Programming 57 06-11-2013 10:09 PM
Lightroom Export, am I doing this the hard way? J. Clarke Digital Photography 0 05-05-2013 08:15 PM
Really throwing this out there - does anyone have a copy of my oldDancer web browser? Python 1 04-10-2013 03:32 PM
GL2 better than the XLs? Consumer grade HDs better than pro-sumer Mini DVs? dh@. DVD Video 1 08-28-2008 07:20 PM
Is splint really better than lint? Is there a better tool than splint? Peter Bencsik C Programming 2 09-21-2006 10:02 PM