Hole type: subroutine
Write a Perl subroutine which returns the fifth word in a string. A
word is any sequence of letters, numbers, and/or underscores. The
string will be passed as the sole argument and will contain at least 5
words.
Code | Length |
---|---|
sub hole {(pop=~/\w+/g)[4]} | 16 |
This was intended as a practice hole for the tournament. This hole provides some hints to help with the later holes. If there's one argument in @_, pop is the shortest way to get at it; and many golf problems can be solved with a short regular expression.
This hole also shows how tricky these golf holes can be. A correct solution should return the word 'fifth' for the following string: '!first@@second#third$$fourth%fifth^^sixth'. In this string, the words are not broken by whitespace; the words are broken by multiple non-word characters; and the first word is preceeded by a non-word character.