CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
A list of if's rather then 10 elsif

 



JFrost
Novice

May 21, 2001, 7:44 AM

Post #1 of 3 (746 views)
A list of if's rather then 10 elsif Can't Post

Hi,

I have a series of 10 fields that I want to check if they have data in them. If any of them do, I want to perform a task. Here is the problem, I have 10 elsif's and it get's very cluttered. Is there a way to have a if list. So that if one the if's are true it will do the task.

I hope you understand the question

Thanks






mhx
Enthusiast / Moderator

May 21, 2001, 8:12 AM

Post #2 of 3 (745 views)
Re: A list of if's rather then 10 elsif [In reply to] Can't Post

> I hope you understand the question

No ;-)

Perhaps you post your code excerpt with the elsif's or the complete script and we'll see.

-- Marcus



Jasmine
Administrator / Moderator

May 21, 2001, 9:36 AM

Post #3 of 3 (742 views)
Re: A list of if's rather then 10 elsif [In reply to] Can't Post

I'm not sure I understand either, but I'll give it a shot anyway.

Let's say your input is in a hash.


Code
my %fields = ( 
1 => '',
2 => '2',
3 => '',
4 => '4',
);

my $count = grep{ $_ } values %fields;

Above is a simple hash with some empty entries. Then, using grep, we assign $count with the count of true values in $fields. $count will be 0 ( false ) if there's no true values entered. So you can use:


Code
if ( $count ){ 
# values exist, do something.
}

Hope this helps!


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives