Re: Short or Long Hand
by Joost (Canon) on Aug 12, 2004 at 15:24 UTC
|
Clear and 100% equivalent:
if($day != 0 && $day != 6) {
| [reply] [d/l] |
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! | [reply] [d/l] [select] |
|
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
| [reply] [d/l] |
|
That won't quite work--you need parens.
print $day, $/ if $day !~ /^(?:0|6)$/;
or
print $day, $/ if $day !~ /^[06]$/;
| [reply] [d/l] [select] |
|
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.
| [reply] [d/l] |
|
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
| [reply] |
Re: Short or Long Hand
by BrowserUk (Patriarch) 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
| [reply] [d/l] [select] |
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>
| [reply] [d/l] |
|
I think you're aiming for obfuscation there. The grep operator returns the number of matches in a scalar context, so you could have just said:
unless (grep $day == $_, 0, 6) {
...
}
| [reply] [d/l] |
|
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.
| [reply] [d/l] [select] |
|
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
| [reply] |
|
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.
| [reply] |
Re: Short or Long Hand
by jdporter (Paladin) 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.
| [reply] [d/l] [select] |
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
| [reply] [d/l] |
Re: Short or Long Hand
by shemp (Deacon) on Aug 12, 2004 at 16:01 UTC
|
| [reply] |
|
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 | [reply] [d/l] [select] |
|
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
| [reply] [d/l] |
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))
| [reply] [d/l] |
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. | [reply] [d/l] |
|
if ($day > 0 && $day < 6) {
I think is pretty clear.
| [reply] [d/l] |