sub oneof { my $item = $_[0]; # The item to search for in the array. my $lstr = $_[1]; # List of items in string separated by commas my $sepr = $_[2]; # Separator (default is comma) # if the separator was not passed, then default to a comma. if ($sepr =~ /^$/ || $sepr !~ /.{1}/) { $sepr = ","; } # Split the list string into an array for the search. @list = split(/$sepr/,$lstr); $found = 0; # Set found to 0 in case item is not found. # Search through the items in the list array from first (index = 0) # to last (index = number of items in list [ $#list ] - 1) for ($x = 0; $x < $#list; $x++) { if ($item eq $list[$x]) { $found = $x+1; # Set found to index+1 if item is found. last; # Discontinue for loop if found } } return $found; # Return value of found to calling script. }
Example 1: oneof subroutine with comments.