Home > Archive > PERL CGI Beginners > October 2005 > "if" Question
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Vance M. Allen 2005-09-25, 6:55 pm |
| Greetings,
I have several places in my code where I need to check if two string
variables are empty, if one is empty and the other is not, and if they're
both populated.
Is there any simpler way to write the code than this?:
if($var1 ne "" and $var2 ne "") {
# Both are populated...
# ...
}
elsif($var1 ne "" or $var2 ne "") {
# One or the other is populated...
# ...
}
else {
# Neither are populated...
# ...
}
The reason I ask is that some of the code gets to be fairly long when I'm
checking things this way, and so I just wondered if there was any
shorthand/shortcut way to have Perl check if two strings are empty in one
expression...regular expressions, maybe? I appreciate any help,
suggestions, or answer you can provide.
Thanks,
Vance
| |
| Charles K. Clarkson 2005-09-26, 6:55 pm |
| Vance M. Allen <mailto:vma_nntp@vmacs.us> wrote:
: I have several places in my code where I need to check if two
: string variables are empty, if one is empty and the other is
: not, and if they're both populated.
:
: Is there any simpler way to write the code than this?
No. At least there is no perl built-in way to rewrite it. You
could use a subroutine, but you haven't supplied enough details to
get detailed help. For example, if the response is always he same
then this may work.
compare( $var1, var2 );
# or
my @result = compare( $var1, var2 );
sub compare {
my @fields = @_;
if ( $fields[0] eq '' and $fields[1] eq '' ) {
# Neither are populated...
# ...
} elsif ( $fields[0] eq '' or $fields[1] eq '' ) {
# One is populated...
# ...
} else {
# Both are populated...
# ...
}
}
It's important, though, that the subroutine not work on
external variables. Variables not passed into the sub.
HTH,
Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328
| |
| David Luke 2005-09-28, 6:55 pm |
| Hi Vance,
This sounds silly, but Why do you need to know? If your operation is as
simple as trying to decide whether or not to insert a comma or other
separator, you can do that with the JOIN command without checking the
individual values.
$result = join(',' ($var1, $var2));
$result will be:
"" or "$var1" or "$var2" or "$var1,$var2"
You also have the inline if:
$result = ($var1 ne "" ? ($var2 ne "" ? "Both" : "Var1Only") : ($var2 ne ""
? " Var2Only " : "Neither");
$result will be one of:
"Both", "Var1Only", "Var2Only", "Neither"
David Luke, Application Developer IV
Isocorp, Inc.
DMS Enterprise Information Technology Services
100 Rhyne Building
2740 Centerview Drive
Tallahassee, Florida 32301
(850) 216-3746
-----Original Message-----
From: Vance M. Allen [mailto:vma_nntp@vmacs.us]
Sent: Sunday, September 25, 2005 2:42 AM
To: beginners-cgi@perl.org
Subject: "if" Question
Greetings,
I have several places in my code where I need to check if two string
variables are empty, if one is empty and the other is not, and if they're
both populated.
Is there any simpler way to write the code than this?:
if($var1 ne "" and $var2 ne "") {
# Both are populated...
# ...
}
elsif($var1 ne "" or $var2 ne "") {
# One or the other is populated...
# ...
}
else {
# Neither are populated...
# ...
}
The reason I ask is that some of the code gets to be fairly long when I'm
checking things this way, and so I just wondered if there was any
shorthand/shortcut way to have Perl check if two strings are empty in one
expression...regular expressions, maybe? I appreciate any help,
suggestions, or answer you can provide.
Thanks,
Vance
| |
| Paul Lalli 2005-10-07, 7:55 am |
| David Luke wrote:
> Hi Vance,
>
> This sounds silly, but Why do you need to know? If your operation is as
> simple as trying to decide whether or not to insert a comma or other
> separator, you can do that with the JOIN command without checking the
> individual values.
>
> $result = join(',' ($var1, $var2));
>
> $result will be:
> "" or "$var1" or "$var2" or "$var1,$var2"
Where did you get that idea from? join doesn't care if its arguments
are empty strings, or even if they're undefined.
my $var1 = '';
my $var2 = 'not empty';
my $result = join(',', ($var1, $var2));
print "Result: $result\n";
__END__
Result: ,not empty
Paul Lalli
| |
| Paul Lalli 2005-10-07, 7:55 am |
| Vance M. Allen wrote:
> I have several places in my code where I need to check if two string
> variables are empty, if one is empty and the other is not, and if they're
> both populated.
>
> Is there any simpler way to write the code than this?:
>
> if($var1 ne "" and $var2 ne "") {
> # Both are populated...
> # ...
> }
> elsif($var1 ne "" or $var2 ne "") {
> # One or the other is populated...
> # ...
> }
> else {
> # Neither are populated...
> # ...
> }
You could use the any() and all() routines from List::MoreUtils:
#!/usr/bin/perl
use strict;
use warnings;
use List::MoreUtils qw/any all/;
my $s1 = '';
my $s2 = 'not empty';
if (any { $_ eq '' } $s1, $s2){
if (all {$_ eq ''} $s1, $s2){
print "S1 and S2 both emtpy\n";
} else {
print "One of S1 or S2 empty\n";
}
} else {
print "Neither S1 nor S2 empty\n";
}
__END__
One of S1 or S2 empty
If you're doing this repeatedly, then simply put this block into a
subroutine, replacing '$s1, $s2' with '@_'
Paul Lalli
|
|
|
|
|