Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: String/Numeric Manipulation

by shenme (Priest)
on Oct 07, 2004 at 17:08 UTC ( #397371=note: print w/replies, xml ) Need Help??

in reply to String/Numeric Manipulation

The first time you use $day is to do a numeric comparison. But you haven't yet made _sure_ that the value is a number. Perhaps you could check first, by doing something like:
if( $days =~ m/^\d+$/ ) { ~ ~ all your code that depends on $days being numeric } else { ~ ~ print nicely nasty message about bad input }

Replies are listed 'Best First'.
Re^2: String/Numeric Manipulation
by Ms. T. (Novice) on Oct 07, 2004 at 19:30 UTC
    Can you explain to me what the translation of m/^\d+$/ ) means? I am totally new to perl and I have read about the m/ to some degree.
      Okay, but first, the others are correct in having you check for your 'q' quit action first.

      The '^' character says start matching from the very first character in the string. This is called an 'anchor', because you're saying the match starts at a specific place.

      The '\d' means match only number characters. The translation is '[0123456789]' also seen as '[0-9]'. There are many of these shortcuts and it is worth your while to learn them (see perlre doc).

      The '+' means "one or more of the preceding". So we're saying there has to be at least one numeric digit and maybe more (but contiguous - nothing in-between).

      And the '$' is like '^', an anchor, saying the match ends after the last character in the string. (There's a footnote about newline chars, but look that up later).

      So altogether we're saying that the entire string (because of the '^' and '$' anchors) must contain only numeric characters and there must be at least one of those, and nothing else is allowed.

      Other people have mentioned playing with '+' or '-' signs and that's reasonable. It gets into the topic "what do you think a number looks like?" I know there's good discussions in lots of books. I found a bit of one in perlretut under "Building a Regexp". There's probably others in the docs.

      With REs it is only a question of "How much fun can you stand at any one time?" Learn just what you need at the time and keep adding on!

Re^2: String/Numeric Manipulation
by revdiablo (Prior) on Oct 07, 2004 at 23:02 UTC

    Check out perldoc -q whole, or an online version. It contains a few other regular expressions, some of which may be more applicable to the problem at hand. It also mentions looks_like_number from Scalar::Util, which may be a nice alternative to regular expression checks.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://397371]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2022-01-24 07:58 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (64 votes). Check out past polls.