Forum Moderators: coopster & phranque

Message Too Old, No Replies

More fun with shortcuts, did any condition match?

         

csdude55

5:28 pm on Sep 22, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm pretty sure the answer to this is "no", but since there've been a lot of nifty shortcuts added since I began then it doesn't hurt to ask :-)

I have several conditions set up like:

if ($foo eq 'bar') {
$this = 'that';
}

elsif ($lorem eq 'ipsum') {
$example = 'something else';
}


where there could be literally hundreds of elsif statements. There's no guarantee that a "true" will change the same variable.

The question is, is there a way to say "if any of the above if / elsif conditions matched, then do this"?

The only option I know to do is to add another variable, like so:

$match = 0;

if ($foo eq 'bar') {
$this = 'that';
$match = 1;
}

elsif ($lorem eq 'ipsum') {
$example = 'something else';
$match = 1;
}

if ($match) {
# do this
}


Do you guys and gals know of a way to do this without using the additional variable?

Brett_Tabke

5:38 pm on Sep 22, 2021 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



You can do some fun stuff with the Switch/Case statement, but your example is about as straight ahead and maintainable code as you will find. I do that exact same thing very often. I do alot of this too:

$match++ if $foo eq "bar";

I also skip

if ($match) {
# do this
}

and instead do:

return (0) if !$match;
then fall through to the 'true' code.

I'm a huge huge believer in "everything should be in a subroutine". If I get more than a screen full of mainline code (30statements), I drop it into a subroutine.

[tutorialspoint.com...]

NickMNS

5:44 pm on Sep 22, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



write a function.


#---- psuedo code - I have no idea about Perl syntax ----#
def match_func() {
# do this
return the_something_that_was_done
}
if ($foo eq 'bar') {
$this = 'that';
match();
}

elsif ($lorem eq 'ipsum') {
$example = 'something else';
match()
}

csdude55

6:13 pm on Sep 22, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Oh well, thanks :-)

I was looking through all of the predefined variables, it just FEELS like Perl should have a built-in variable for it! But I guess not. And it seems like a lot of them have high performance hits, anyway.

[perldoc.perl.org...]

I mainly try to prevent the same thing over and over, so having hundreds of $match = 1, $match++ if, or match(); seems like a waste of space.

I'm a huge huge believer in "everything should be in a subroutine". If I get more than a screen full of mainline code (30statements), I drop it into a subroutine.

@Brett_Tabke, I'm usually the same way, but I haven't found a way to do it in this case. I have 10-15 fields entered by users, and then wrote conditions specifically for users that keep breaking rules (like putting something in the wrong category). So there's nothing consistent from one user to the next.

Example:

# Rob keeps entering the wrong phone number
if ($name eq 'robzilla' &&
$tele =~ /1324567/) {
$tele = '1234567';
}

# NickMNS keeps posting JavaScript stuff in the Perl forum
elsif ($name eq 'nickmns' &&
$category eq 'Perl' &&
$body =~ /javascript/i) {
$category = 'Client Side';
$subcategory = 'JavaScript';
}

# csdude's spacebar is broken, he keeps using . or , instead
elsif ($name =~ /^csdude/ &&
$body =~ /[.,]/ &&
$body !~ /\s/) {
($tele, $body, $etc) = csdude_fix($tele, $body, $etc);
sub csdude_fix {
map s/[.,]/ /g, @_;
return @_;
}
}

# someone keeps posting Lucy's phone number
if ($tele =~ /9876543/ ||
$body =~ /9876543) {
$tele = '';
$body =~ s/9876543//g;
}


So these are all ad hoc, fixing recurring problems to keep me from having to fix them manually.

lucy24

6:30 pm on Sep 22, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Can you use a “switch (true)” construction, or is that strictly a javascript thing? (I remember years ago someone hereabouts pointed it out to me because I was lamenting the lack of the argument-less case/select format used in basic.)

At the outset you said elseif. Does that mean that no more than one condition will apply at any given time?

<tangent>
The final list of examples gave me a giggle, because it reminded me of a former forum where one person kept mis-typing the name of a beloved pet so it came out as a vile ethnic slur and people wondered why the pet was named ###. The solution was to deploy Word Filter to replace that specific ethnic slur ... with the pet’s name.
</tangent>

csdude55

7:18 pm on Sep 22, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Inexplicably, Perl did away with switch/case! They replaced it with "given", but even now that one's on the fence:

[perltutorial.org...]

It's infinitely faster to use switch/case than if/else in PHP, so I really have no clue why it was removed :-(

Originally all of these conditions were just "if", but I recently changed them to "if / elsif". Unless I come across an issue in practice, then you are correct that only one condition will apply at any given time. I'm hesitant to rely on that, though, just because it hasn't been fully tested.

The final list of examples gave me a giggle, because it reminded me of a former forum where one person kept mis-typing the name of a beloved pet so it came out as a vile ethnic slur and people wondered why the pet was named

My main site turns 20 years old next year, so I have a HUGE list of those types of examples! In my classifieds, just about everybody misspells "shih tzu", so I have to autofix that, too :-O

The biggest problem I have is with phone numbers, really. Some people make an honest mistake (like they mean to type "123-4456" but instead type "123-4556"), and some are intentionally trying to get someone harassed (by entering a friend or enemy's phone number in a super cheap classified). And I never know about it until someone reports it.

The most common fix is to do something like, "if the username is NOT the pre-approved username and anything contains their phone number, remove the phone number". Or, my own personal favorite, "if the username is X and the phone number is anything other than the confirmed Y, change it to Y". This works well because they inevitably get themselves harassed and delete it fast, and if pushed I can always say, "golly, I'm sorry, I assumed it was a typo and was just trying to help!" LOL

Brett_Tabke

9:13 pm on Sep 22, 2021 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I assume you are familiar with Perls Ternary operator? It is not used in much code I have seen, but boy can it be handy to set flip flops like you wanted:

[perlmaven.com...]

csdude55

11:34 pm on Sep 22, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I've been using ternary for awhile now (started with PHP, only recently started doing it in Perl), but if I understand correctly then it only works if I'm setting a value on the same variable in the if / else. Right?

Eg, this:

if ($foo eq 'bar') { $lorem = 'ipsum'; }
else { $lorem = 'example'; }

becomes:

$lorem = ($foo eq 'bar') ? 'ipsum' : 'example';

In this case, some conditions will set values to one or more of ten variables, and some run a regex on one or more of ten variables. So there's no constant $lorem (for example) between them.