Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Need to match number other than 1

by kprasanna_79 (Hermit)
on Nov 29, 2007 at 10:11 UTC ( #653793=perlquestion: print w/replies, xml ) Need Help??

kprasanna_79 has asked for the wisdom of the Perl Monks concerning the following question:

Revered Monks,
Apologize for this silly question. I need to match a number other than 1. The input can be even four or five digit. But it should allow when it is not number 1
-Prasanna.K

Replies are listed 'Best First'.
Re: Need to match number other than 1
by tachyon-II (Chaplain) on Nov 29, 2007 at 10:22 UTC
    if ( $number =~ m/^\d+$/ and $number != 1 ) { # do stuff } else { die "There can be only 1!" }
Re: Need to match number other than 1
by moritz (Cardinal) on Nov 29, 2007 at 10:28 UTC
    m/\A (?: [1]\d+ # if it starts with a 1, # it has to be followed by other digits | [023456789]\d* # otherwise one digit can be fine ) \z /x

    Note that this matches only non-negative integers, and 01 is matched as well (no idea if that's OK or not, spec is unclear).

    Note that you need some kind of anchoring (perhaps to a word boundary), but you don't necessarily need to anchor it to start and beginning of the string.

Re: Need to match number other than 1
by shmem (Chancellor) on Nov 29, 2007 at 10:19 UTC
    I need to match a number other than 1.

    So you started with matching 1 ? How did you do that?

    Read perlre. While you're at it, read How (Not) To Ask A Question, then post the code you tried.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      Am I missing the point here or isnt it just
      if($number != 1) { }

        You miss the "match number" part.

        qwurx [shmem] ~ > perl -wle '$foo = "bar"; print "not 1" if $foo != 1' Argument "bar" isn't numeric in numeric ne (!=) at -e line 1. not 1

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Need to match number other than 1
by grinder (Bishop) on Nov 29, 2007 at 10:35 UTC
    if ($num =~ /^[^\D1]\d+$/) { print "does not begin with a 1\n"; }

    The key to understanding this is that you are looking for anything that is not a non-digit or a 1. When you negate the negations in the above phrase, you'll see that it means you're matching 0, 2-9.

    uh, update: that doesn't quite work, hang on a sec...

    if ($num =~ /^(?:[^\D1]|1\d)\d*$/) { print "not a 1\n"; }

    2nd update: now it works. It is instructive to see how the pattern had to be modified to exclude 1, but allow 10, 19, 1111, ...

    • another intruder with the mooring in the heart of the Perl

      That doesn't match any single digit number, nor will it match 10, which is not 1 either.
Re: Need to match number other than 1
by mwah (Hermit) on Nov 29, 2007 at 13:02 UTC
    I need to match a number other than 1. The input can be even four or five digit. But it should allow when it is not number 1

    After trying to interpret your question (what is meant by "number 1" anyway), I'll add another version of the "detection of 1 game":

    ... my $num1 = qr{^ (?> ([\.\d]+) ) (??{$1 == '1' ? '(?!)' : '' }) }x; my $txt1 = qr{^ (?> ([\.\d]+) ) (??{$1 eq '1' ? '(?!)' : '' }) }x; my @stuff = qw' 0101 1 123 1.1 01 222 21 1.000 '; print map $_ . "\t as num: " . (/$num1/ ? 'ok' : '--') . "\t as txt: " . (/$txt1/ ? 'ok' : '--') . "\n", @stuff; ...

    Regards

    mwa

      You don't have to generate any code. The (?(COND)TRUE|FALSE) assertion fits the glove here. $^N can be used instead of $1 here, making it copy-paste safe.

      I'd also like to draw some attention to Regexp::Common::number. These changes would make

      my $num1 = qr{^ (?> ([\.\d]+) ) (??{$1 == '1' ? '(?!)' : '' }) }x;
      become
      use Regexp::Common qw/ number /; my $num1 = qr{^ (?> ($RE{num}{dec}) ) (?(?{ $^N != '1' })|(?!)) }x;
      I reversed the logic in the COND block so that it reads better to me. "Match a number, the number isn't 1, done".

      lodin

      I tried that and got this warning:
      Argument "\x{d6d}\x{bef}" isn't numeric in numeric eq (==) at (re_eval + 2) line 1.
      :)
Re: Need to match number other than 1
by GrandFather (Saint) on Nov 29, 2007 at 10:46 UTC
    if ($theNumberUnderTest == 42) { print "Matched a number other than 1\n"; }

    Perl is environmentally friendly - it saves trees
      Yeah, I'd have to say this answers the "question" by testing for the ultimate answer, to life, the universe, and everything; but what was the question?
Re: Need to match number other than 1
by tcf03 (Deacon) on Nov 29, 2007 at 12:58 UTC
    if ( $number =~ /^\d*$/ and $number != 1 ) { # not 1 stuff here }
    Ted
    --
    "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
      --Ralph Waldo Emerson
Re: Need to match number other than 1
by queldor (Acolyte) on Nov 29, 2007 at 13:46 UTC
    reading it as 'Need to match number other than 1 digit'
    if( m{^\d{2,}$} ) { # do stufff }
Re: Need to match number other than 1
by olus (Curate) on Nov 29, 2007 at 14:09 UTC
    Reading it as having a sequence of 4 or 5 digits, as in '12345', and allowing sequences that do not have the '1', then simply:

    my $sequence = '12345'; if( index($sequence, '1') >= 0 ) { # found the 1 digit. Interrupt here. }

    --
    olus
Re: Need to match number other than 1
by logan (Curate) on Nov 29, 2007 at 21:54 UTC
Re: Need to match number other than 1
by nimdokk (Vicar) on Nov 29, 2007 at 20:23 UTC
    I don't know, this may be over-simplistic - but what about:
    if ( $number > 1 ) { print "$number greater than 1.\n"; } elsif ( $number < 1 ) { print "$number less than 1.\n"; } else { print "$number is 1.\n"; }
Re: Need to match number other than 1
by hangon (Deacon) on Nov 30, 2007 at 08:49 UTC

    "Sometimes a cigar is just a cigar" Freud

    if ($number - 1){ #do sometning }
Re: Need to match number other than 1
by iomatrix (Initiate) on Nov 30, 2007 at 00:57 UTC
    Easier to ask if it isn't 1 then if it is. if (not ($var == 1)) { ...do whatever... } Chris (iomatrix)
Re: Need to match number other than 1
by xicheng (Sexton) on Dec 01, 2007 at 08:37 UTC
    how about:
    if ($str =~ /^(?!1$)\d+$/) { do_stuff($str); }
    Regards,
    Xicheng

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2021-04-22 14:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?