Determine whether the array contains a value (Perl)


In Perl, as a way to check whether the values contained in the array is to use a hash.

# Sample array.
my @list = ('microsoft','apple','google');
# Value to search
my $serachString = 'apple';

# Create a hash bowl
my %tmp_hashvalues = ();

# Array of hashes. As the value "1" to have a.
for (@list) {
$tmp_hashvalues{$_} = 1;
}

# If the hash contains search string, it returns 1.
if($tmp_hashvalues{$serachString} eq 1){
//Do something when it was included
}

Tags: Perl


HOME > Articles 2011 > determine whether a value contains an array (Perl)

HOMEHOME CategoryCategory HistoryHistory

Site Search :

[HTML link code]