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