Four is the number of letters in the ...: Difference between revisions

Added Perl example
(Added Perl example)
Line 375:
Length of sentence = 70995756
</pre>
 
=={{header|Perl}}==
Uses <code>Lingua::EN::Numbers</code> module to generate number names. State variable in <tt>extend_to</tt> routine keeps track of last word tallied.
{{trans|Perl 6}}
<lang perl>use feature 'state';
use Lingua::EN::Numbers qw(num2en num2en_ordinal);
 
my @sentence = split / /, 'Four is the number of letters in the first word of this sentence, ';
 
sub extend_to {
my($last) = @_;
state $index = 1;
until ($#sentence > $last) {
push @sentence, split ' ', num2en(alpha($sentence[$index])) . ' in the ' . no_c(num2en_ordinal(1+$index)) . ',';
$index++;
}
}
 
sub alpha { my($s) = @_; $s =~ s/\W//gi; length $s }
sub no_c { my($s) = @_; $s =~ s/,//g; return $s }
sub count { length(join ' ', @sentence[0..-1+$_[0]]) . " characters in the sentence, up to and including this word.\n" }
 
print "First 201 word lengths in the sequence:\n";
extend_to(201);
for (0..200) {
printf "%3d", alpha($sentence[$_]);
print "\n" unless ($_+1) % 32;
}
print "\n" . count(201) . "\n";
 
for (1e3, 1e4, 1e5, 1e6) {
extend_to($_);
print
ucfirst(num2en_ordinal($_)) . " word, '$sentence[$_-1]' has " . alpha($sentence[$_-1]) . " characters. \n" .
count($_) . "\n";
}</lang>
{{out}}
<pre>First 201 word lengths in the sequence:
4 2 3 6 2 7 2 3 5 4 2 4 8 3 2 3 6 5 2 3 5 3 2 3 6 3 2 3 5 5 2 3
5 3 2 3 7 5 2 3 6 4 2 3 5 4 2 3 5 3 2 3 8 4 2 3 7 5 2 3 10 5 2 3
10 3 2 3 9 5 2 3 9 3 2 3 11 4 2 3 10 3 2 3 10 5 2 3 9 4 2 3 11 5 2 3
12 3 2 3 11 5 2 3 12 3 2 3 11 5 2 3 11 3 2 3 13 5 2 3 12 4 2 3 11 4 2 3
9 3 2 3 11 5 2 3 12 4 2 3 11 5 2 3 12 3 2 3 11 5 2 3 11 5 2 3 13 4 2 3
12 3 2 3 11 5 2 3 8 3 2 3 10 4 2 3 11 3 2 3 10 5 2 3 11 4 2 3 10 4 2 3
10 3 2 3 12 5 2 3 11
1203 characters in the sentence, up to and including this word.
 
One thousandth word, 'in' has 2 characters.
6048 characters in the sentence, up to and including this word.
 
Ten thousandth word, 'in' has 2 characters.
61055 characters in the sentence, up to and including this word.
 
One hundred thousandth word, 'the' has 3 characters.
627301 characters in the sentence, up to and including this word.
 
One millionth word, 'five' has 4 characters.
6781802 characters in the sentence, up to and including this word.</pre>
 
=={{header|Perl 6}}==
2,392

edits