I was watching the Empire Strikes Back this evening and I
wanted to see if I could write a sub to translate a normal
sentence into Yoda-speak (read: too much free time). So here it
is. I am welcome to suggestions, improvments, or more pivot words.
grep
#!/usr/local/bin/perl -w use strict; my @sentences = ('I will teach you', 'The truth is out there', 'That is an Imperial shuttle', 'My husband is giant dork', #this is my wife's s +uggestion 'These fish are tasty' ); foreach (@sentences) { print yoda($_)."\n"; } 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]} grep {$_->[1] > 0} map { [" $_ ",index($sentence," $_ ")] } @pivot_word +s)[0]; #^^^^^^ ^^^^^^^ # Change to fix the pr +oblem [zdog] # pointed out - THX [z +dog] # No pivot words return $sentence if (!$pivot); # Pivot the sentence $sentence = substr($sentence,$pivot->[1]+length($pivot->[0]),lengt +h($sentence)). " ". substr($sentence,0,$pivot->[1]). $pivot->[0]; # Clear leading spaces $sentence =~ s/^\s+//; # Sentence case $sentence = ucfirst(lc($sentence)); }
Update: Fixed to handle pivot words inside another word.
Update 2: Fixed the word boundry problem with a better solution. The new sub follows:
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]} @pi +vot_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)); }
grep
Unix - where you can throw the manual on the keyboard and get a command |
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Yoda Speak Translator
by Fletch (Bishop) on Apr 26, 2002 at 09:51 UTC | |
| |
Re: Yoda Speak Translator
by Putzfrau (Beadle) on Apr 26, 2002 at 17:24 UTC | |
by darksym (Beadle) on Apr 27, 2002 at 21:42 UTC | |
Re: Yoda Speak Translator
by Dog and Pony (Priest) on Apr 26, 2002 at 15:25 UTC | |
by jonadab (Parson) on Apr 17, 2003 at 19:18 UTC | |
Re: Yoda Speak Translator
by grep (Monsignor) on Apr 30, 2002 at 01:06 UTC | |
Yoda Speak is Everywhere
by Guildenstern (Deacon) on May 17, 2002 at 17:46 UTC | |
(zdog) Re: Yoda Speak Translator
by zdog (Priest) on Apr 28, 2002 at 05:16 UTC | |
Re: Yoda Speak Translator
by ChOas (Curate) on May 02, 2002 at 10:23 UTC | |
Re: Yoda Speak Translator
by $name (Pilgrim) on Apr 29, 2002 at 18:44 UTC | |
Re: Yoda Speak Translator - well it's official
by grep (Monsignor) on Jun 17, 2002 at 03:19 UTC | |
by adrianh (Chancellor) on Aug 18, 2002 at 09:23 UTC | |
by Anonymous Monk on Aug 16, 2002 at 07:19 UTC | |
by Anonymous Monk on Sep 22, 2003 at 09:51 UTC | |
by Anonymous Monk on Sep 22, 2003 at 13:40 UTC | |
by kirolindo (Initiate) on Jun 13, 2012 at 14:39 UTC | |
| |
by Anonymous Monk on May 14, 2004 at 14:51 UTC | |
Re: Yoda Speak Translator
by raven_bd (Initiate) on May 03, 2002 at 09:53 UTC | |
by Anonymous Monk on Dec 04, 2003 at 19:59 UTC | |
Re: Yoda Speak Translator
by Anonymous Monk on Jul 31, 2018 at 14:35 UTC |
Back to
Cool Uses for Perl