Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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$/;

Add:
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!


Comment on Re: Short or Long Hand
Select or Download Code
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
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)$/;
    or
    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 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 :)

    --
    <http://www.dave.org.uk>

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2014-12-25 10:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (160 votes), past polls