分页: 1 / 1

perl printf 格式的存储

发表于 : 2014-10-22 16:56
imlxq
在最下面的代码中,两个sub只有printf的格式不一样,因次我想到如果能够这样调用就更好了,我试了试,没有找到实现方法,求大神们出招~~ :em11

代码: 全选

&dealmode("VICA","$data[2] $data[3] 0 0 $data[0]\n");
&dealmode("VICR","$data[1] $data[2] $data[3] $data[4] $data[5] $data[6] $data[7] $data[8] $data[9]\n");

代码: 全选

&dealmodeA("VICA");
&dealmodeB("VICR");

sub dealmodeA{
	open(VCP_DATA,"<$data_dir/$vcp_path.$_[0]") || die("can't open VCP_DATA_$_[0]!!!");
	open(VCP_DATA_TEMP,">$data_dir/VCP_data.$_[0].temp") || die("can't open VCP_DATA_$_[0]!!!");
	while (<VCP_DATA>) {
		chomp;
		s/^\s*//;
		s/\s*$//;
		s/\s+/ /g;
		unless (/^$|^\/\/|^\#/) {
			@data = split( / /, $_ );
			printf VCP_DATA_TEMP "$data[2] $data[3] 0 0 $data[0]\n";
		}
	}
	close VCP_DATA;
	close VCP_DATA_TEMP;
}

sub dealmodeB{
	open(VCP_DATA,"<$data_dir/$vcp_path.$_[0]") || die("can't open VCP_DATA_$_[0]!!!");
	open(VCP_DATA_TEMP,">$data_dir/VCP_data.$_[0].temp") || die("can't open VCP_DATA_$_[0]!!!");
	while (<VCP_DATA>) {
		chomp;
		s/^\s*//;
		s/\s*$//;
		s/\s+/ /g;
		unless (/^$|^\/\/|^\#/) {
			@data = split( / /, $_ );
			printf VCP_DATA_TEMP "$data[1] $data[2] $data[3] $data[4] $data[5] $data[6] $data[7] $data[8] $data[9]\n";
		}
	}
	close VCP_DATA;
	close VCP_DATA_TEMP;
}

Re: perl printf 格式的存储

发表于 : 2014-10-25 0:01
lhw828
路过,帮顶

Re: perl printf 格式的存储

发表于 : 2014-11-03 17:34
astolia

代码: 全选

sub max {
  my($max_so_far) = shift @_;
  foreach (@_) {
    if ($_ > $max_so_far) {
      $max_so_far = $_;
    }
  }
  $max_so_far;
}
$max1 = &max(3, 5, 10, 4, 6, 11);
$max2 = &max(3, 5, 10);

print "$max1\n$max2\n"

Re: perl printf 格式的存储

发表于 : 2014-11-08 23:57
imlxq
:em01 这是个求最值的啊,和问题木有啥关系,貌似
astolia 写了:

代码: 全选

sub max {
  my($max_so_far) = shift @_;
  foreach (@_) {
    if ($_ > $max_so_far) {
      $max_so_far = $_;
    }
  }
  $max_so_far;
}
$max1 = &max(3, 5, 10, 4, 6, 11);
$max2 = &max(3, 5, 10);

print "$max1\n$max2\n"

Re: perl printf 格式的存储

发表于 : 2014-11-09 1:15
astolia
看走眼了,你这种情况该用eval

代码: 全选

sub p {
	my @data = (1, 2, 3);
	print eval $_[0];
}

p('"$data[0] + $data[1] = $data[2]\n"');
p('"$data[1] - $data[0] = $data[0]\n"');