M
Mark
Hi all,
I tried to find the way to search the first occurence of a string into
a list (which is a sub-set of a file) in order to know if this string
occurs at least one time anywhere.
I've read the perldoc -f grep and perldoc -f index but it seems that
these two functions does not allow me to do it in a simple way.
So do I have to write my own index function ?
I tried something like the following, but it seems that I do not handle
properly the line I passed to the function:
#!/usr/bin/perl -w
#!C:\Perl\bin\perl.exe -w
use strict;
use warnings;
use Benchmark;
my @f_lines = (<DATA>);
my $i = "aaaaaef";
my ($count, $start, $end);
print "\n$0: String searched is $i\n\n";
&start_bm;
$count = grep (/$i/i, @f_lines) ;
&print_bm("GREP: returned $count, ");
&start_bm;
$count = &my_index ("$i",@f_lines) ;
&print_bm("MY_INDEX: returned $count, ");
sub my_index() {
# $_[0]: searched string
# $_[1]: list
my $tag = $_[0];
my @list = $_[1];
my $n;
foreach (<@list>) {
$n = index($_, /$tag/i);
if ($n) {
return 1;
}
}
return 0;
}
##
## Functions
##
sub start_bm { $start = new Benchmark; }
sub print_bm() {
$end = new Benchmark;
printf $_[0]."%s\n", timestr(timediff($end, $start),'auto');
}
###########################################################################
TIA for any help,
MA
I tried to find the way to search the first occurence of a string into
a list (which is a sub-set of a file) in order to know if this string
occurs at least one time anywhere.
I've read the perldoc -f grep and perldoc -f index but it seems that
these two functions does not allow me to do it in a simple way.
So do I have to write my own index function ?
I tried something like the following, but it seems that I do not handle
properly the line I passed to the function:
#!/usr/bin/perl -w
#!C:\Perl\bin\perl.exe -w
use strict;
use warnings;
use Benchmark;
my @f_lines = (<DATA>);
my $i = "aaaaaef";
my ($count, $start, $end);
print "\n$0: String searched is $i\n\n";
&start_bm;
$count = grep (/$i/i, @f_lines) ;
&print_bm("GREP: returned $count, ");
&start_bm;
$count = &my_index ("$i",@f_lines) ;
&print_bm("MY_INDEX: returned $count, ");
sub my_index() {
# $_[0]: searched string
# $_[1]: list
my $tag = $_[0];
my @list = $_[1];
my $n;
foreach (<@list>) {
$n = index($_, /$tag/i);
if ($n) {
return 1;
}
}
return 0;
}
##
## Functions
##
sub start_bm { $start = new Benchmark; }
sub print_bm() {
$end = new Benchmark;
printf $_[0]."%s\n", timestr(timediff($end, $start),'auto');
}
###########################################################################
TIA for any help,
MA