#將字串移除最後三個字元並返回
sub rm3LastStr {
@inputStrArr = split ("",$_[0]);
pop(@inputStrArr); pop(@inputStrArr); pop(@inputStrArr);
$resultStr = join("",@inputStrArr);
return $resultStr ;
}
#將字串分成多個三字元字串 並整合成陣列
sub strToStorage{
$strInput = $_[0];
$leng = length ($strInput); #字串長度
@strArr = split("",$strInput); #將輸入字串轉成字元陣列
# $L3start = length($strInput) -4 ;
# $L3end = length($strInput) -1 ;
while ($leng >3){
@rm3Ch = @strArr[length($strInput) -3 .. length($strInput) -1]; #取出陣列的最尾三個字元
print "\@rm3Ch state : @rm3Ch\n";
$rm3chStr = join("",@rm3Ch);#將取出的最尾字完陣列轉成字串
print "\$rm3shStr state :$rm3chStr\n";
push (@storage, $rm3chStr); #將此三字元字串置於陣列@storage中
$strInput = rm3LastStr($strInput);#移除字串中最後三個字元
print "\$strInput state -> $strInput \n";
$leng -=3;#字串長度減三
if ($leng <=3){
$remain = $strInput;
push (@storage,$strInput);
}
}
@goodStorage = reverse @storage;
return @goodStorage;
}
print "\n------------\n";
@getIt = strToStorage($money);
print "\@getIt => @getIt\n";
$formatted = join(",",@getIt);
print "\$$formatted";
沒有留言:
張貼留言