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:
List problem! urgent!

 



Peter Van Hoecke
Deleted

Apr 27, 2000, 3:52 AM

Post #1 of 4 (500 views)
List problem! urgent! Can't Post

Hello,

is there a statement to test if a value is already in a list?
take this example:

@family = qw( peter guido marjolijn luke);

peter <***element of***> @familie && print "Welcome to the family!";

this solution is error prone:
(( join " ", @familie ) =~ /peter/ ) && print "Welcome to the family!";

Any ideas?
Peter


japhy
Enthusiast

Apr 27, 2000, 4:08 AM

Post #2 of 4 (500 views)
Re: List problem! urgent! [In reply to] Can't Post

From Perl FAQ 4:

"How can I tell whether a list or array contains a certain element?

Hearing the word 'in' is an indication that you probably should have used a hash, not a list or array, to store your data."

Suggestion:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


@seen{@array} = ();
if (exists $seen{$element}) {
print "$element is in the array";
}
else {
print "$element is not in the array";
}
</pre><HR></BLOCKQUOTE>


Peter Van Hoecke
Deleted

Apr 27, 2000, 4:41 AM

Post #3 of 4 (500 views)
Re: List problem! urgent! [In reply to] Can't Post

You're probably wright, but it's actually for the "small database 2..." problem. (cfr @sec)

There is a first (ugly) version of my script, if you have suggestions, it would make me very happy...

Peter


Peter Van Hoecke
Deleted

Apr 27, 2000, 11:44 PM

Post #4 of 4 (500 views)
Re: List problem! urgent! [In reply to] Can't Post

grep {/peter/} @familie && print 'Yep, you're in the family!';

 
 


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

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