Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am a perl newbie and have a problem with the index function.
I am searching a string for the word 'Lecht'. The string I am searching contains 'lech' and 'lecht'.
The position I receive is of the first string which is 'lech', not the one I want.
Is there a way to combine regex with the index function so it searches just for the full string 'lecht'?
Heres' what i am using:
$search_string = 'lecht';
$string_position = index($line,$search_string);

Thanks

Replies are listed 'Best First'.
Re: index() problem
by Roy Johnson (Monsignor) on Feb 05, 2004 at 22:31 UTC
    Post a small chunk of code (where $line is set) that demonstrates the problem. index should not be partial-matching.

    The PerlMonk tr/// Advocate
      OK, the aim of the code is to go through the first file (news.txt) and where it finds 'The Lecht' then match it to 'The Lecht' in the second file (resort_list_for_parser.txt).
      Once matched it should then add in a appropriate anchor tag.

      It seems to be grabing the data from the string 'Lech' instead.
      Here's the code as requested:

      The file's is opened and $line is set here:

      open(NEWS,"news.txt") || die "Cannot open file"; my @news_lines = <NEWS>; close (NEWS); open(RESORT_LIST,"resort_list_for_parser.txt") || die "Cannot open fil +e"; my @resort_list_lines = <RESORT_LIST>; close (RESORT_LIST); foreach $line (@news_lines) { $line =~ s/\n\r//sgi; $line =~ s/\r//sgi; $line =~ s/\n//sgi;
      Within the foreach above the follwing code exists:
      foreach $resort_line (@resort_list_lines) { my ($resort_lookup,$resort,$country) = split (/:/,$resort_ +line,3); $resort =~ s/\r//sgi; $resort =~ s/\n//sgi; $resort_string_position = index($line,$resort_lookup); if ($resort_string_position != -1) { my $resort_lookup_length = length($resort_lookup); substr($line, $resort_string_position, $resort_lookup_l +ength) = "<html formatting>$resort_lookup</html formatting>"; } } }

      Edit by tye: Replace BR with CODE tags

        If index was the problem, you should have been able to demonstrate it in two or three lines of code. We don't know what your data file(s) contain(s) (and I for one don't care to know), but you should be able to debug this program by throwing in some debug print statements to display some of those variables before you do the index, like $line and $resort_lookup.
        A general note: Your uses of s/// are overkill. The s and i options are useless on \r and \n, and you could do the same thing with one tr/[\r\n]//d

        We monks still can't see what the values are in the variables in question. What I meant for you to provide was an example that we could run to observe the problem. Explicitly set the value of $line to whatever it is when you see the problem, include the relevant lines of code, and show the output, along with what you expected instead.


        The PerlMonk tr/// Advocate
Re: index() problem
by welchavw (Pilgrim) on Feb 06, 2004 at 02:50 UTC

    Two approaches, one simple, one less so...

    my $v = "lech_lecht"; #APPROACH ONE - single regex $cnt = 0; $v =~ m{ .*? (?{$cnt++}) lecht (?{$cnt+=4}) }x; print "$cnt\n"; #APPROACH TWO - feed regex output to length() $v =~ /.*?lecht/; print length $&, "\n"; __OUTPUT__ 10 10

    Note, that for position, you may want to subtract one from the result, as these approaches give 1-based answers.

    ,welchavw