Gossamer Forum
Home : General : Perl Programming :

Syntax Question (in list - multiple fields)

Quote Reply
Syntax Question (in list - multiple fields)
I've got a list of ~30 fields I need to check for a value. Whats the best way to do this. Example:

if (($Field1 ne "0.00") || ($Field2 ne "0.00") || ad nauseum...) {print "hey the fields aren't empty"}

I could do:

if (($Field1 > "0") || ($Field2 > "0") || ...($Field30 > "0")) {blah}

is there a better way?

Ps: The fields aren't simply 1-30 or I'd try to do a loop of some kind. Instead they jump all over the place.

Just curious...
Quote Reply
Re: [Watts] Syntax Question (in list - multiple fields) In reply to
well... at the risk of supressing "use strict" requirements, you can try variable variables..

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy logic] Syntax Question (in list - multiple fields) In reply to
Thanks. I 'bit the bullet' and just listed them all out for now. They aren't sequential but they are group pretty close together. I think I'll look into doing some kind of range or loop anyway.
Quote Reply
Re: [Watts] Syntax Question (in list - multiple fields) In reply to
Maybe this?

Code:

use CGI;
my $IN = new CGI;

my (@field,@values);
$field[0] = 'Field1'; $values[0] = '30';
$field[1] = 'Field2'; $values[1] = '50';
$field[2] = 'Field3'; $values[2] = '32';
$field[3] = 'Field4'; $values[3] = '3';
$field[4] = 'Field5'; $values[4] = '20';
$field[5] = 'Field6'; $values[5] = '10';

for (my $i = 0; $i <= $#fields; $i++) {
if ($IN->param($field[$i]) < $values[$i]) {
print "ERROR on field $field[$i] <BR>";
}
}

Untested, but I don't see why it wouldn't work.

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!

Last edited by:

Andy: Mar 10, 2004, 12:04 AM
Quote Reply
Re: [Andy] Syntax Question (in list - multiple fields) In reply to
Quote:
I don't see why it wouldn't work

I do Tongue

@fields has no values... and I'm not sure if

Code:
my $field[0]

is even valid syntax.

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy logic] Syntax Question (in list - multiple fields) In reply to
I tried doing a range, but it didn't work Frown.

It would have been really cool if I could have done:

if (($field1 .. $field30) =~ /bob/)) {
print "hello bob";
}

but I think ranges are only for the opposite side of the equation.
Quote Reply
Re: [Watts] Syntax Question (in list - multiple fields) In reply to
Assuming the values of your fields are in an array named @fields, you can do
Code:
if(grep(/bob/,@fields)) {
print "hello bob";
}

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Syntax Question (in list - multiple fields) In reply to
Many thanks. I think this will do the trick. I'm running the script "locally" so I don't think I'll notice any impact by searching through all of the fields (about 900 or so) several times.
Quote Reply
Re: [Watts] Syntax Question (in list - multiple fields) In reply to
Yikes! 900 fields? What exactly does your program do?

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy logic] Syntax Question (in list - multiple fields) In reply to
It really not as bad as it sounds... One of the software programs we use stores records in individual 'flat files' that are formated with a 'field number' and a 'value' (like such):

1,"Bob"
2,"Smith"
3,"1313 Mockingbird Lane"

So the individual files (records) are only like 20k each but have approx 900 lines.

I use Perl to open a particular file, and do some error checking such as:
if line 47 equals "Male" and line 58 equals "Mrs." the print an error message that sex and salutation don't match.

Stuff like that. It's for legal documents that get recorded so they have to be right. We use it for checking dates, numbers and the kind of stuff Perl is good at doing. It saves us big bucks in not having to correct stupid mistakes later on.