Re: How to chck for string in array elements



folkvord@xxxxxxxxx wrote:
Hi!

I am trying to make an index of a lot of textfiles. Since the index
thends to get quite large, I want to optimize my algorithm som that if
$string is a part of a element already stored in @words it won't be
added to the array.

As an example:

my @words=qw(this is just an example of words);
$string = "his";

Since "his" is a substring of "this", it shouldnt be neccessary to
index it.

Your assertion seems strange to me, "his" is a valid English word that I'd expect to be able to look up in an index. Presumably you know what you are doing?

But I cant figure out an efficient way to check if $string is
a substring of an element in the array.

Can anyone give me a hint her, please ?


I've no idea how efficient it is, but maybe this counts as a hint?

#!/usr/bin/perl
use strict;
use warnings;

my @words=qw(this is just an example of words);
my $string = "his";

for my $word (@words) {
if ($word =~ /$string/) {
print "$string is a substring of $word\n";
}
}

You may need to post a minimal but complete working example of your current code if you need help optimising it.

HTH
.



Relevant Pages

  • Comments on Comments (was Re: Getting to 100 (#119))
    ... # yield each partitioning of the receiver into count partitions ... # an initial substring of increasing length, ... # the string into count-1 partitions. ... #:ops - an array of strings representing the operators to be inserted into ...
    (comp.lang.ruby)
  • Re: Open File For Input Method Faster Than File System Object Stream?
    ... Much depends on exactly what you are doing, and the best method to advise would depend chiefly on whether you are interested in only the first occurrence of the substring in the file or whether you want to check every line of the file for that substring. ... Dim s1 As String, fn As Long ... You could then use Instr in a loop to run through the entire string just once, searching for and recording (in an array of Longs) the position of every linefeed character it finds. ...
    (microsoft.public.vb.general.discussion)
  • Re: Regex zum Trennen von geklammerten Termen
    ... Leerstrings im Array. ... "The array returned by this method contains each substring of this string ...
    (de.comp.lang.java)
  • Re: How to chck for string in array elements
    ... thends to get quite large, I want to optimize my algorithm som that if ... But I cant figure out an efficient way to check if $string is ... a substring of an element in the array. ... you'd also have to throw out words that are a substring ...
    (comp.lang.perl.misc)
  • Re: How to chck for string in array elements
    ... thends to get quite large, I want to optimize my algorithm som that if ... But I cant figure out an efficient way to check if $string is ... a substring of an element in the array. ...
    (comp.lang.perl.misc)