Hi all,
I've found some codes from 'Mastering Algorithms with Perl' O'Reilly.
{
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
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