sub yoda { my ($sentence) = @_; my @pivot_words = qw/is be will show do try are teach have/; # Find out if I have a pivot word and grab the one with the lowest index my $pivot = (sort { $a->[1] <=> $b->[1]} # sort the lowest index to the top grep {$_->[1] > 0} # filter out non-matches map{$sentence =~ /\b$_\b/ ? [$_,$+[0]] : [$_,0]} @pivot_words)[0]; # create an anon array in the format # ['pivot word',index_of_where_it_is] # # @+ stores the indexes of the successful matches and does not # require the gyrations that [pos] does # No pivot words return $sentence if (!$pivot); # Pivot the sentence $sentence = substr($sentence,$pivot->[1]). " ". substr($sentence,0,$pivot->[1]); # Clear leading spaces $sentence =~ s/^\s+//; # Sentence case $sentence = ucfirst(lc($sentence)); }