Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: extracting numbers with a regex

by dws (Chancellor)
on Dec 05, 2001 at 09:13 UTC ( #129528=note: print w/replies, xml ) Need Help??

in reply to extracting numbers with a regex

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?

Replies are listed 'Best First'.
Re: Re: extracting numbers with a regex
by Fastolfe (Vicar) on Dec 05, 2001 at 11:25 UTC
    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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://129528]
[Corion]: Writing these import plugins was really nice though - in about 2 hours, I had imports for CSV, YAML and JSON, and adding XLS(X), SQLite (or DBI) data sources is also trivial. I'm idly wondering about separating the plugin into transport+parser, so ...
[Corion]: ... http:// URLs could be retrieved and then parsed, but I think that that would be overkill for a toy static site generator ;)
[Corion]: This is a toy clone of Jekyll, currently named MrHyde. But maybe I'll change that name to Jokel / Yokel, or some other play on the name , still mildly unfunny ;)

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2018-05-22 11:10 GMT
Find Nodes?
    Voting Booth?