Re: Short and easy way to write if...elsif syntax
by moritz (Cardinal) on Aug 27, 2012 at 09:30 UTC
|
If you have a non-ancient version of Perl 5, this should work:
use 5.010;
foreach (@array)
{
when (/linux/) { say "This is linux VM" }
when (/Windows/) { say "This is Windows VM" }
default { say "I have no idea what it is" }
}
| [reply] [d/l] |
|
I wasn't aware of the "say" function. thanks for the suggestion and also thanks for the perl6 link that you have provided at the bottom of your response..
| [reply] |
Re: Short and easy way to write if...elsif syntax
by GrandFather (Saint) on Aug 27, 2012 at 11:14 UTC
|
Depending on your real application, it may be that a dispatch table would serve you well. Consider:
use strict;
use warnings;
my %oss = (linux => \&isLinux, Windows => \&isWindows, undef => \&isOt
+her);
my @array = qw(linux Windows wibble);
exists $oss{$_} ? $oss{$_}->() : $oss{undef}->() for @array;
sub isLinux {
print "This is linux VM" . "\n";
}
sub isWindows {
print "This is a Windows VM" . "\n";
}
sub isOther {
print "I have no idea what it is" . "\n";
}
Prints:
This is linux VM
This is a Windows VM
I have no idea what it is
True laziness is hard work
| [reply] [d/l] [select] |
Re: Short and easy way to write if...elsif syntax
by grizzley (Chaplain) on Aug 27, 2012 at 09:55 UTC
|
%versions=
(
'linux' => sub { print "This is linux VM\n" },
'Windows' => sub { print "This is a Windows VM\n" }
);
# create this list once before loop
my $idlist = join "|", keys %versions;
@array = ('linux', 'Windows', 'OSX');
foreach(@array)
{
if(/$idlist/)
{
my $id = $&;
&{$versions{$id}}();
}
else
{
print "I have no idea what it is\n";
}
}
Update: please be careful with this approach, as you will have problems if one sysname is included in another one, e.g. 'Windows' and 'Windows 7' (well, actually this is a problem in your original if / elsif list, too.) | [reply] [d/l] |
Re: Short and easy way to write if...elsif syntax
by davido (Cardinal) on Aug 27, 2012 at 19:27 UTC
|
This is one of Perl's classical answers to the "switch" statement. In fact, if someone had simply created another name for "foreach" called "switch", we might have been spared the confusion that given has brought upon us.
foreach ( @array ) {
/linux/ && do {
print "This is a linux VM.\n";
next;
};
/Windows/ && do {
print "This is a Windows VM.\n";
next;
};
/Other/ && do {
print "I have no idea what it is.\n";
next;
};
die "You shouldn't be seeing this: No pattern matched.\n";
}
| [reply] [d/l] [select] |
Re: Short and easy way to write if...elsif syntax
by Athanasius (Archbishop) on Aug 27, 2012 at 11:57 UTC
|
For the problem as given, the following simple approach may be useful:
#! perl
use strict;
use warnings;
my @array = ('Mastering linux is a steep learning curve',
'Using Windows can be frustrating',
'Plan 9 is rarely seen');
print /linux/ ? "This is linux VM\n" :
/Windows/ ? "This is a Windows VM\n" :
"I have no idea what it is\n" for @array;
Output:
This is linux VM
This is a Windows VM
I have no idea what it is
See Conditional Operator. But note that this approach is not generally scalable for dealing with more complex conditions. And see the thread if statement confusion for the syntactic pitfalls.
Hope that helps,
Athanasius <°(((>< contra mundum
| [reply] [d/l] [select] |
Re: Short and easy way to write if...elsif syntax
by pvaldes (Chaplain) on Aug 27, 2012 at 13:37 UTC
|
my @array = ("linux", "milinus","Windows","windows","Linux");
foreach (@array){
if (/((linux|Windows))/i){ print "This is $1 VM" . "\n" }
else { print "no idea" . "\n" }
}
... mmmh, or even:
foreach (@array){/((linux|Windows))/i ? print "This is $1 VM\n": print "no idea\n"}
(in fact you don't need an array for this)
print "THIS is $^O\n";
Therefore you could still eliminate two or three characters:
foreach (@array){/(($^O|Windows))/i ? print "This is $1 VM\n": print "no idea\n"} | [reply] [d/l] [select] |
|
thank you very much everyone for your time. I will be learning quite a few Perl feature which were discussed in this thread. thanks again.
| [reply] |
Re: Short and easy way to write if...elsif syntax
by BillKSmith (Monsignor) on Aug 27, 2012 at 12:14 UTC
|
use strict;
use warnings;
$_ = 'foofum';
do{
my $print_buffer = /linux/ ? 'This is a linux VM'
: /Windows/ ? 'This is a Windows VM'
: 'I have no idea what it is'
;
print $print_buffer, "\n";
};
| [reply] [d/l] |
Re: Short and easy way to write if...elsif syntax
by LanX (Saint) on Aug 28, 2012 at 08:58 UTC
|
no need to repeat $_ =~ and just add next to avoid fall thru.
for ($given){
print "\nFOR($_):";
if (/abc/) { print "abc"}
if (/def/) { print "def" ;next}
if (/xyz/) { print "xyz" ;next}
print "default";
}
more examples at Understanding the benefit of Given/When ...
| [reply] [d/l] [select] |
Re: Short and easy way to write if...elsif syntax
by sundialsvc4 (Abbot) on Aug 27, 2012 at 11:14 UTC
|
Does it work? Is it crystal clear? Is it maintainable?
If the next condition that needs to be tested, for the next version of the software or for the next wild-hare idea that comes in from Marketing, is slightly different, will the logic still answer all of the preceding questions in the affirmative? (Hint: “No.”)
| |
|
To the “down-voters” among you, who may mistake brevity for lack of wisdom or experience, I invite your reconsideration. Here’s why.
The “clever improvements” as suggested cause each of the various if cases to become coupled. In other words, as long as each and every if-case that could possibly be required, for the entire service lifetime of this application (which could be a decade or more), is identical ... the code is “clever,” and perhaps it may look a wee bit more agreeable to the digestion.
However ...
Change will come. Some day, a condition will need to be added that will break the rule. And, when that happens, suddenly the code that is working properly now must be torn-apart, more or less, and recoded. What was meant to be clever has just turned bad, and it de-stabilized all of the logic that it “cleverly” tied together. Whereas, if the “ugly” if..elsif structure had simply been retained, no changes to any of the existing code, with its admittedly repetitive structure, would have been required. You would simply need to add another elsif block at the appropriate point. You can keep that up indefinitely.
Please bear in mind that I have spent most of my career in “code rescue” and project-turnarounds. Which means that my perspective on such things is somewhat like that of a coroner. Such “cleverness,” well-intentioned though it once may have been, after a surprisingly short amount of time attracts a large number of blowflies. Maintainability is king, and software must be designed in anticipation of a very long service life involving many different people. You won’t be at that job forever ... but your work will be, and someday I might be called in to have a look.
| |
|
| [reply] |
|
|
|
| [reply] |