我想在perl脚本中修改一个特定的行:
@list = ([$tmp[0],$tmp[1],$tmp[2]]);
Run Code Online (Sandbox Code Playgroud)
我把它改成了这个:
if( $input == 3 )
{
@list = ([$tmp[0],$tmp[1],$tmp[2]]);
}
if( $input == 2 )
{
@list = ([$tmp[0],$tmp[1]]);
}
if( $input == 1 )
{
@list = ([$tmp[0]]);
}
Run Code Online (Sandbox Code Playgroud)
上面的代码工作,但我希望它适用于$input最多40的值.
我尝试了几种不同的方法,但我无法以更优雅的方式对其进行编码,这真的很烦我.
非常感谢任何帮助.
以下是您所要求的:
@list = [ @tmp[ 0 .. $input-1 ] ];
Run Code Online (Sandbox Code Playgroud)
以下是您想要的:
my @list = @tmp[ 0 .. $input-1 ];
Run Code Online (Sandbox Code Playgroud)
您还可以使用以下方法非常有效地就地执行后者:
splice(@tmp, $input);
Run Code Online (Sandbox Code Playgroud)