我有一个要求,我需要在循环内执行第一次出现变量的语句.
例如:给定数组我@rand_numbers = qw(1 2 1 2 3 1 3 2);
知道数组中只有3个值(即在本例中为1,2和3)
我想在每个值的第一次遇到时打印某些东西(或做某事)(仅在第一次遇到并且永远不会重复它,以便连续遇到相应的值).
以下是一种方法
my @rand_numbers = qw(1 2 1 2 3 1 3 2);
my $came_across_1=0, $came_across_2=0, $came_across_3=0;
for my $x(@rand_numbers) {
print "First 1\n" and $came_across_1=1 if($x==1 and $came_across_1==0);
print "First 2\n" and $came_across_2=1 if($x==2 and $came_across_2==0);
print "First 3\n" and $came_across_3=1 if($x==3 and $came_across_3==0);
print "Common op for -- $x \n";
}
Run Code Online (Sandbox Code Playgroud)
有没有办法在没有变量的情况下达到上述结果$came_across_x?[即在触发器操作员的帮助下?]
谢谢,Ranjith