Gossamer Forum
Home : General : Perl Programming :

better way to do it?

Quote Reply
better way to do it?
Hi,

I'm checking fields prior to processing and I've got it working fine, but I wonder if there's a better (more efficient) way to do it? I'll probably be adding 3-5 more values/fields also.

if ((($form{'field'} eq "Value1") or ($form{'field'} eq "Value2") or ($form{'field'} eq "Value3")))) {
$spread = "file.html";
}
else {
$spread = "file2.html";
}

You think this will work ok with about 5-9 values? Any comments or suggestions would be great! Thank you.

Quote Reply
Re: better way to do it? In reply to
How about this?

as a loop...
Code:
foreach ($val1, $val2, $val3) {
if ($form{'field'} eq $_) {
$spread = "file.html";
} else {
$spread = "file2.html";
}
}
as a regexp...
Code:
if ($form{'field'} =~ /^($val1|$va2|$val3)$/) {
$spread = "file.html";
} else {
$spread = "file2.html";
}
Happy coding,

--Drew
http://www.camelsoup.com
ftp://ftp.camelsoup.com
Quote Reply
Re: better way to do it? In reply to
Nice..thanks junko! Any ideas which way is faster...or is it all pretty much going to be the same? Actually, I just tried the regexp method, and it seems slightly faster?? Who knows, for nothing else, it's much easier and shorter coding though, thank you very much!

Quote Reply
Re: better way to do it? In reply to
Hmm, I thought that method worked, but I was wrong. I don't think I'm going to fool around with it anymore though..I guess as long as I have the original way working that's ok for now! Basically, it's not accepting any of the values.....it's just throwing all of them to file2.html. But if you have any ideas as to what I could have done wrong (please share, cuz maybe I can learn something) that would be cool...if not no big deal. Thanks again.

if ($form{'type'} =~ /^($silver|$bronze|$gold|$platinum|$diamond|$copper)$/) {
$spread = "file.html";
} else {
$spread = "file2.html";
}

Quote Reply
Re: better way to do it? In reply to
#1 wouldn't work correctly..

Code:
foreach ($val1, $val2, $val3) {
if ($form{'field'} eq $_) {
$spread = "file.html";
last;
}
else {
$spread = "file2.html";
}
}
would however.. :)

sorry for being an [bleep!].. haha.. thanks for tagging the tagboard junko.

Jerry Su
http://www.jsu07.com
Quote Reply
Re: better way to do it? In reply to
umm.. i'm guessing those $silver, etc things are scalars.. i don't see why they aren't working but i'm taking a quick guess.. try fixing the problem like this..

Code:
if ($form{'type'} =~ /^(${silver}|${bronze}|${gold}|${platinum}|${diamond}|${copper})$/) {
$spread = "file.html";
}
else {
$spread = "file2.html";
}
Jerry Su
http://www.jsu07.com
Quote Reply
Re: better way to do it? In reply to
I tried Jerry Blush Can't believe I forgot 'last'. duh.

Happy coding,

--Drew
http://www.camelsoup.com
ftp://ftp.camelsoup.com
Quote Reply
Re: better way to do it? In reply to
Um, err, ok, I'm a retard...I couldn't get either of those two methods to work! Ah, oh well, at least I've got the sluggish way (my way) going...that'll work for now. Thanks guys, I appreciate the help!