Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Short or Long Hand

by Dietz (Curate)
on Aug 12, 2004 at 15:25 UTC ( #382295=note: print w/replies, xml ) Need Help??

in reply to Short or Long Hand

Why not just use a simple regex:

print $day, $/ if $day !~ /^0|6$/;

I made a common beginner mistake and I feel really ashamed about it.

Although above code will work on a single digit as it was demanded
( $day representing a weekday field from localtime() )
it is not logic as it says: don't match 0 at the beginning or don't match 6 at the end.

With nonmatching parens ( !~ /^(?:0|6)$/ ) it will satisfy the logic, still matching nondigits though. So it would've even been better written as !~ /0|6/ since the expected data is a single digit.

I'm so sorry!

Replies are listed 'Best First'.
Re^2: Short or Long Hand
by davorg (Chancellor) on Aug 12, 2004 at 15:33 UTC

    Maybe because regexes are harder to get right.

    If you're not 100% sure that $day will always be between 0 and 6 (and I'm not sure you ever can be!) then you'd be better off with:

    print $day, $/ if $day !~ /^(0|6)$/;

    Precendence can be a problem :)


    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re^2: Short or Long Hand
by Eimi Metamorphoumai (Deacon) on Aug 12, 2004 at 15:30 UTC
    That won't quite work--you need parens.
    print $day, $/ if $day !~ /^(?:0|6)$/;
    print $day, $/ if $day !~ /^[06]$/;
      It will work without parens as long as $day is a weekday field ranging 0..6
      Char class ( [06] ) is another good idea since it is faster then the alteration but you wouldn't determine the speed diff on a single digit.
Re^2: Short or Long Hand
by Scarborough (Hermit) on Aug 12, 2004 at 15:28 UTC
    I like that one too, Im not that great at regex but it looks like I should do a little homework
    Thanks for your help

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://382295]
[james28909]: everything past the first two things you piked from my posts are irrelevant to me and my beliefs. they are side effects.
[james28909]: two or three. if it has to do with how humans are handling said situations, then that is after they were able to evolve into those problems. agnostic means only believe with evidence. i took that from a tyson video
[james28909]: and it seems that racism suggest that there is indeed a better race, but that is for evolution to decide
[1nickt]: "right now, humans are smarter than in any of those times": define "smart"
[erix]: ah, now you're appraoch downvote-territory ;)

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (11)
As of 2017-12-15 14:57 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (433 votes). Check out past polls.