|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
|
|
#1
|
|||
|
|||
|
How to determine if a value is already in an array?
Of course I can do it with a loop and compare, but I suspect there must be easier ways. (forgive me, I am a perl rookie). Thanks in advance. |
|
#2
|
|||
|
|||
|
See the grep function:
http://www.perldoc.com/f?grep @matches = grep { /this_pattern_from/ } @values Sample usage: my @ary = qw(one two three); my $val = 'test'; if (grep { /$val/ } @ary) { print "it existsn"; } else { print "it does not existn"; } <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by liupang: How to determine if a value is already in an array? Of course I can do it with a loop and compare, but I suspect there must be easier ways. (forgive me, I am a perl rookie). Thanks in advance.[/quote] |
|
#3
|
|||
|
|||
|
I think simplest way is to use hashes :
@a = qw {one two three}; $var = 'test'; @seen{@a}=1; print "$var already exists" if $seen{$var}; <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by liupang: How to determine if a value is already in an array? Of course I can do it with a loop and compare, but I suspect there must be easier ways. (forgive me, I am a perl rookie). Thanks in advance.[/quote] ------------------ Regards, Andrew ================== http://afixxer.boom.ru |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > Simple question |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|