我正在制作一个将英语句子转换为Pig Latin的程序(因为我很无聊)。代码看起来非常好,但是当我输入一个句子时它会抛出错误。我不知道为什么会出错。
守则:
use strict;
use warnings;
sub to_pig_latin
{
my $sentence = shift;
my @words = split(" ", $sentence);
my $translated = " ";
my $cw;
for(my $i = 0; $i < scalar(@words); $i++)
{
my $word = $words[$i];
if($word eq "." or $word eq "!" or $word eq "?")
{
}
else
{
$cw = substr(1, length($word)-1, $word);
$translated = $translated.$cw.substr(0,1,$word)."ay"." ";
}
}
return $translated;
}
print "Welcome to English -> Pig Latin Converter.\n";
print "Enter an English sentence to translate:\n";
my $to_translate = <STDIN>;
chomp($to_translate);
print " ";
my $translated = to_pig_latin($to_translate);
print "Translated sentence:$translated\n";
错误:
Welcome to English -> Pig Latin Converter.
Enter an English sentence to translate:
Hello World!
Argument "Hello" isn't numeric in substr at pig_latin.pl line 18, <STDIN> line 1.
substr outside of string at pig_latin.pl line 18, <STDIN> line 1.
Use of uninitialized value $cw in concatenation (.) or string at pig_latin.pl line 19, <STDIN> line 1.
Argument "World!" isn't numeric in substr at pig_latin.pl line 18, <STDIN> line 1.
substr outside of string at pig_latin.pl line 18, <STDIN> line 1.
Use of uninitialized value $cw in concatenation (.) or string at pig_latin.pl line 19, <STDIN> line 1.
Translated sentence: ay ay
答案 0 :(得分:7)
文档说:
substr EXPR,OFFSET,LENGTH
你的论点被颠倒了。