in reply to Re^2: Newbie Q:How do I compare items within a string?
in thread Newbie Q:How do I compare items within a string?

A tidier alternative to my pos() - length($1) is to consult @- .

push @{$positions{lc($1)}}, $-[1] while $string =~ /([A-Za-z']+)/g;
The difference in indexing is that your code is matching on seperator characters instead of word characters. The end of your first match is the start of my second.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^4: Newbie Q:How do I compare items within a string?
by johngg (Canon) on May 09, 2006 at 10:23 UTC
    I don't think that's the difference. I split on separator characters when forming the array @words but I negate the character class when doing the s{ ... }{ ...}xeg to add the annotation. Thus, like you, I am pulling out words but by capturing one or more non-separator characters.

    Cheers,

    JohnGG

    Update: I substituted your pattern

    ([A-Za-z']+)(?{++ $found{lc $1}})

    for my pattern

    ([^.,;:?! \n]+)(?{++ $found{lc $1}})

    and the results were identical.