Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Short or Long Hand

by Scarborough (Hermit)
on Aug 12, 2004 at 15:15 UTC ( #382280=perlquestion: print w/ replies, xml ) Need Help??
Scarborough has asked for the wisdom of the Perl Monks concerning the following question:

Is there a short hand for this
if((not $day == 0) && (not $day == 6)){ #do something like push onto an array }
Im interested after reading Legible or Obfuscated? and think that I should write my quick test programs using Obfuscation/Shorthand call it as you like . Any thoughts?

Comment on Short or Long Hand
Download Code
Re: Short or Long Hand
by jdporter (Canon) on Aug 12, 2004 at 15:17 UTC
    Well, assuming the range for $day is 0..6, you could say
    if ( $day % 6 )
    But this is probably a good example of code that is short, and not particularly obtuse, but far less clear in what it is trying to do.
Re: Short or Long Hand
by davorg (Chancellor) on Aug 12, 2004 at 15:18 UTC
    if ($day % 6) { # ... }

    But that will also be true if $day is 12, 18, etc...

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

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

Re: Short or Long Hand
by rsteinke (Scribe) on Aug 12, 2004 at 15:19 UTC

    Not so useful for a short list like this with only two elements, but

    if(not @{[grep {$day == $_} (0,6)]}) { # foo }
    Not much of a gain here, but imagine if the list were 20-30 elements.

    Ron Steinke
    <rsteinke@w-link.net>
      I like that one, I have a use for that when I'm working out my month end dates which in my case are not always the last day of the month.
      Thanks
        If you are doing much date manipulation at all, you should strongly consider using one of the fine modules available: DateTime, Date::Calc, Date::Manip, etc. There isn't much learning curve; most have FAQs with cut-and-paste recipes for common tasks.
        I like that. Although it's a good argument for something like an in operator. if ($day in (0,6)) is (to me) much cleaner in meaning, even if it's just syntactic sugar for the grep.
Re: Short or Long Hand
by Scarborough (Hermit) on Aug 12, 2004 at 15:23 UTC
    See what you guys are saying but I know $day will be 0..6 as it comes from the weekday field from localtime(). What I'm looking for is the days 1..5. Which I think I get from the code above.
Re: Short or Long Hand
by Joost (Canon) on Aug 12, 2004 at 15:24 UTC
Re: Short or Long Hand
by Dietz (Curate) on Aug 12, 2004 at 15:25 UTC
    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!
      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
      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.

      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

Re: Short or Long Hand
by shemp (Deacon) on Aug 12, 2004 at 16:01 UTC
    After seeing a number of posts suggesting using a regex to accomplish this task, i am left a bit surprised. It seems to me that in terms of program speed, doing a couple numeric comparisons will be faster than a regex or two. This problem itself is somewhat trivial, but in general i see a lot of regex solutions here at perlmonks, which seem to be for the sake of terse code, but will probably take longer to execute in general than other alternatives.

    Am i missing something here?

      i see a lot of regex solutions here at perlmonks, which seem to be for the sake of terse code, but will probably take longer to execute in general than other alternatives
      That's because computer time has a tendancy to be cheaper than programmer time. Of course, trading one hour of programmer time for a few days (or months or years) of computer time may well be worth it. This is the delicate balance of when optimization is worthwhile.
      ------------ :Wq Not an editor command: Wq

      Regular expressions are attractive for this sort of problem, because they can express a number of constraints in a single pattern. In this case a regexp such as /[06]/ is slightly slower than the two numerical tests (at least on my local perl installation), but we're talking fractions of a microsecond, and the OP gives no reason to assume that speed is a concern.

      More relevant is what will make the code easier to read and maintain, and that usually means making the code reflect the reality behind the check as expressively as possible. Accordingly, I'd be tempted to write something like:

      use constant SATURDAY => 6; use constant SUNDAY => 0; if ($day != SATURDAY && $day != SUNDAY) { ... }
      .. but that might be overkill. :)

      Hugo

Re: Short or Long Hand
by BrowserUk (Pope) on Aug 12, 2004 at 16:15 UTC

    if( not ( $day == 0 or $day == 6 ) ) { ...

    if( $day and $day != 6 ) { ...

    unless( $day == 0 or $day == 6 ) { ...


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
Re: Short or Long Hand
by inman (Curate) on Aug 12, 2004 at 16:52 UTC
    You are testing for the value of $day falling in the range 1..5 using a negative test (do something unless one of the conditions is true). It will read better if you turn it into a positive test.
    if (($day >= 1) && ($day <= 5))

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2014-10-20 23:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (93 votes), past polls