Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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.


#!/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

In reply to Yoda Speak Translator by grep

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (16)
    As of 2015-07-07 20:42 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (93 votes), past polls