Hi Tom, > On 03 Jul 2015, at 14:07, Tom Browder <tom.brow...@gmail.com> wrote: > > I originally had problems with the S32 description of string function > index. S32 says that if the substring is not found then a bare Int is > returned which evaluates to false, otherwise an Int with the position > of the first substring match is returned. It goes on to say that one > should not evaluate the result as a number. So my question was, how > does one practically use that information for a substring that starts > at position zero which evaluates as false? > > Based on the S32 description, I first tried this to remove a comment > from a data line: > > my $str = '# a comment'; > my $idx = $str.index('#'); > if $index && $index >= 0 { > $str = $str.substr(0, $idx); > } > > It didn't work for a comment at position zero but it found one at any > other position or no comment at all. Then I tried this: > > if $idx { > $str = $str.substr(0, $idx); > } > > Same result as the previous method: it would not report a substring at > position zero. > > So what am I doing wrong for finding position zero substrings?
Apart from what Carl and yary said, I would like to add that *if* you’re just interested in knowing whether a string starts with a certain substring, you can use .starts-with: my $str = ‘# a comment!’; say “It’s a comment” if $str.starts-with(“#”); Similarly, if you’re interested in a certain substring at the end of a string, you can use .ends-with: say “Exclamated” if $str.ends-with("!”); Finally, if you’re interested in a substring at another position in a string, you can use .substr-eq: say “Comment at position 4” if $str.substr-eq(“comment”,4); Hope this helps! Liz