Sure it is. perlretut has a nice introduction, you should read it, especially the part about backreferences. | [reply] |
| [reply] |
| [reply] |
This example fails because it'll also return true if non-alphabetic characters match. You specified "letters".
sub first_character_matches {
return( ((lc shift) ^ (lc shift)) =~ /^\0/ );
}
And a command-line version to play with:
1) poerbook:~% perl -wle 'print "yes" if ( ((lc shift) ^ (lc shift)) =
+~ /^\0/ )' abc DBC
1) poerbook:~% perl -wle 'print "yes" if ( ((lc shift) ^ (lc shift)) =
+~ /^\0/ )' abc ABC
yes
| [reply] [d/l] [select] |
>perl -wMstrict -le
"print '----- output -----';
while (my ($word1, $word2) = splice @ARGV, 0, 2) {
print qq{:$word1: and :$word2: initial letters },
first_letter_matches($word1, $word2)
? 'same' : 'different';
}
sub first_letter_matches {
my ($w1, $w2) = @_;
return $w1 =~ m{ \A ([^\W\d_]) }xmso
&& $w2 =~ m{ \A $1 }xmsi
;
}
"
a a a A Ab abc b b abcd A A1% a2#
"" "" "" a a "" a b a bc ab c 123 123 %# %#
----- output -----
:a: and :a: initial letters same
:a: and :A: initial letters same
:Ab: and :abc: initial letters same
:b: and :b: initial letters same
:abcd: and :A: initial letters same
:A1%: and :a2#: initial letters same
:: and :: initial letters different
:: and :a: initial letters different
:a: and :: initial letters different
:a: and :b: initial letters different
:a: and :bc: initial letters different
:ab: and :c: initial letters different
:123: and :123: initial letters different
:%#: and :%#: initial letters different
| [reply] [d/l] [select] |