#!/usr/bin/perl
#filename linear2.pl
# we'll use a subroutine instead
use strict;
my @names=qw;
my @animals=qw;
my $input;
my $animalInput;
my $i;
my $where;
do
{
print "Enter a name to find ";
$input = ;
chomp($input);
$where=&findit(@names, $input);
if ($where>=0)
{
print "Found at $where\n";
}
else
{
print "Name not found\n";
}
} while ($input ne "stop");
$animalInput="fox";
$where=&findit(@animals,$animalInput);
print "fox found at $where\n";
# subroutine findit
# input - an array of string and a string value seaarch key
# returns the index of search key in the array. returns -1 if not found
sub findit
{
my $key;
my @listofnames;
my $j;
$key=pop(@_);
@listofnames=@_;
for($j=$#listofnames ; $j>=0 ; $j--)
{
if ($listofnames[$j] eq $key)
{
last;
}
}
$j;
}