Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Don't ask to ask, just ask
 
PerlMonks  

extracting numbers with a regex

by coolmichael (Deacon)
on Dec 05, 2001 at 08:32 UTC ( #129524=perlquestion: print w/ replies, xml ) Need Help??
coolmichael has asked for the wisdom of the Perl Monks concerning the following question:

I am using a regex to extract some numbers from a string, to build an array of numbers. However, I don't seem to be able to use < and > to compare the numbers. How can I (or can I) force a numeric context on to $2 and $3 in the annonymous array?

my @list = qw(M4568-51 M4568-5 M8954-89 M4568-56 H1234-6 H0012-45); my @big = map { m/^(\w)(\d{4})-(\d{1,2})$/ or die "bad data: $!\n"; [$_, $1, $2, $3]; } @list; print Dumper(@big); ----------------------results (tidied up a bit)------- $VAR1 = ['M4568-51', 'M', '4568', '51']; $VAR2 = ['M4568-5', 'M', '4568', '5' ]; $VAR3 = ['M8954-89', 'M', '8954', '89']; $VAR4 = ['M4568-56', 'M', '4568', '56']; $VAR5 = ['H1234-6', 'H', '1234', '6' ]; $VAR6 = ['H0012-45', 'H', '0012', '45'];
Thank you.

Comment on extracting numbers with a regex
Download Code
Re: extracting numbers with a regex
by belg4mit (Prior) on Dec 05, 2001 at 08:36 UTC
    Please see Regexp::Common/Regexp::Common. This will help you from reinventing the wheel, and allow you to capture a wider array of numbers.

    Also I see no < or > in your code. So it is difficult to give any further advice...

    --
    perl -p -e "s/(?:\w);([st])/'\$1/mg"

Re: extracting numbers with a regex
by dws (Chancellor) on Dec 05, 2001 at 09:13 UTC
    I am using a regex to extract some numbers from a string, to build an array of numbers. However, I don't seem to be able to use < and > to compare the numbers.

    The conversion from strings to numbers is automagic, though you could write:   $VAR1 = ['M4568-51', 'M', 0 + '4568', 0 + '51' ];
    to force the conversion.

    Can you post a fragment of code that demonstrates the problem?

      I'd even go so far to recommend against doing 0 + '4568' because it's really pretty meaningless. A scalar is a scalar. If there's a number in there, use it like a number and it will act like a number. The only time we should have to resort to 0+ trickery is when there's some ambiguity (e.g. we want to treat "123abc" as "123" numerically, or "0E0" for similar reasons) or we need to do some black magic. Using it unnecessarily complicates code needlessly and makes it harder to read. I've actually seen code where the coder wrote something like: $i = "123"+0 because he didn't quite understand that the two data types were interchangeable in Perl. Scary!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2014-04-21 00:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (489 votes), past polls