本文介绍了Perl将值从一个子例程传递到另一个子例程.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个子程序,该子程序将值传递给另一个子程序.在第二个子例程中,我尝试对使用$ _传递给它的参数进行操作,但此方法不起作用.
如果我尝试在第二个子例程中打印$ _,则会得到传递给第一个子例程的参数字符串.传递传入参数的正确方法是什么?
我正在使用的代码如下:
I have a subroutine that passes a value to another subroutine. In the second subroutine I try to operate on the argument that was passed to it by using $_ and this is not working.
If I try to print out $_ in the second subroutine, I get the argument string that was passed to the first subroutine. What is the proper way to get at the argument passed in???
The code I''m using is below:
sub splitFn{
my @splitArray = split(/;/,$_);
my $retList = ();
my %hashOut = ();
my $i = 0;
my $j = 0;
for ($i = 0; $i < scalar(@splitArray); $i++){
$splitArray[$i] =~ s/^\s+//; #remove leading white spaces ;
$splitArray[$i] =~ s/\s+$//; #remove trailing white space s/s+$//;
}
if (scalar(@splitArray) == 2){
$hashOut{"name"} = $splitArray[0];
$hashOut{"FirstRange"} = $splitArray[1];
}
elsif (scalar(@splitArray) == 3){
$hashOut{"name"} = $splitArray[0];
$hashOut{"FirstRange"} = arrayMkr($splitArray[1]);
$hashOut{"SecondRange"} = arrayMkr($splitArray[2]);
}
else{
die "\n ************** Input file is not formatted correctly! At line $splitArray[$i] $!\n";
}
$retList =(\%hashOut); # Creating a reference to the hash that will be passed back.
return($retList);
}
sub arrayMkr{
my @splitUpIn = split(" ",$_);
my $size = scalar(@splitUpIn);
推荐答案
sub splitFn{
my @splitArray = split(/;/,
这篇关于Perl将值从一个子例程传递到另一个子例程.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!