Gossamer Forum
Home : Products : DBMan : Discussions :

problem on linear search (perl programming)

Quote Reply
problem on linear search (perl programming)
Hi all,

I've found some codes from 'Mastering Algorithms with Perl' O'Reilly.

Code:
sub linear_string
{
my ($array, $target) = @_;
for (my $i = @$array; $i--;)
{
print("$array->[$i]\n");
return $i if $array->[$i] eq $target;
}
return undef;
}

$path= 'd:\\atlog.txt';
open(LOGFILE, "< $path")
or die "Couldn't open $path for reading: $!\n";

$target = 'test';
while (<LOGFILE>)
{
@string= split(/\s+/, $_);
$index = linear_string( \@string, $target);
print("@string($index)");
print("$index\n");
}

It doesn't work! I mean it cannot search my 'target' string. Also it cannot print $index...

My 'atlog.txt' does contain the 'target' string.

Any idea?

Thanks
Cary

Last edited by:

caryhung: Apr 25, 2002, 7:49 PM
Subject Author Views Date
Thread problem on linear search (perl programming) caryhung 4668 Apr 25, 2002, 7:47 PM
Thread Re: [caryhung] problem on linear search (perl programming)
LoisC 4566 Apr 25, 2002, 9:10 PM
Thread Re: [LoisC] problem on linear search (perl programming)
caryhung 4560 Apr 26, 2002, 12:50 AM
Thread Re: [caryhung] problem on linear search (perl programming)
LoisC 4560 Apr 26, 2002, 1:45 AM
Post Re: [LoisC] problem on linear search (perl programming)
caryhung 4543 Apr 28, 2002, 5:19 AM