use strict; use warnings; use Test::More; $| = 1; sub holli { my $imax = 0; foreach (@_) { $imax = ($0 .. !$0) if $_ > $_[$imax]; } return $imax; } is( holli(1,2,13,4,5), 2, "holli(1,2,13,4,5): seems to work"); is( holli(1,2,13,4,5), 2, "holli(1,2,13,4,5): except that if you run it a second time, it will fail, because ff holds its state from last instance"); is( do{local @_ = (1,2,13,4,5); my $imax=0; foreach(@_){$imax = ($0..!$0) if $_>$_[$imax]}; $imax }, 2, "do(1,2,13,4,5): same data set, but use a local do{} instead of the function to avoid the again bug"); is( do{local @_ = (1,2,13,4,5,99); my $imax=0; foreach(@_){$imax = ($0..!$0) if $_>$_[$imax]}; $imax }, 5, "AnomalousMonk(1,2,13,4,5,99): this data set fails, I think because"); is( do{local @_ = (3,2,1,3,2,1,3,2,1,15); my $imax=0; foreach(@_){$imax = ($0..!$0) if $_>$_[$imax]}; $imax }, 9, '(3,2,15): should be 9, but is 1, because ff-if-cond only triggers once'); is( do{local @_ = (1,0,13); my $imax=0; foreach(@_){$imax = ($0..!$0) if $_>$_[$imax]}; $imax }, 2, "do(1,0,13): second element is lower rather than higher than first, so third is the first time that if-then-ff triggers"); is( do{local @_ = (1,0,13,4); my $imax=0; foreach(@_){$imax = ($0..!$0) if $_>$_[$imax]}; $imax }, 2, "do(1,0,13,4): you would think this would fail, too... but because \$_=4 happens to be greater than \$_[\$imax]=\$_[1]=0, it fakes it into passing, because of where values ended up."); ### here's my updated algorithm, to fix the couting errors and off-by-one, but it does not fix the hold-state-between-runs ### ### also, note, I couldn't come up with a way to run the flipflop every time, but assign to imax only on conditional, without storing the ff result in a separate counter... in which case, the counter-based loops make more sense than a flipflop anyway. is( do{ local @_ = (1,2,13,4,5); my $imax=0; foreach(@_){my$ff = $0 .. !$0; $imax = $ff-1 if $_ > $_[$imax]}; $imax;}, 2, "pryrt: do(1,2,13,4,5)" ); is( do{ local @_ = (1,2,13,4,5,99); my $imax=0; foreach(@_){my$ff = $0 .. !$0; $imax = $ff-1 if $_ > $_[$imax]}; $imax;}, 5, "pryrt: do(1,2,13,4,5,99)" ); is( do{ local @_ = (3,2,1,3,2,1,3,2,1,15); my $imax=0; foreach(@_){my$ff = $0 .. !$0; $imax = $ff-1 if $_ > $_[$imax]}; $imax;}, 9, "pryrt: do(3,2,1,3,2,1,3,2,1,15)" ); is( do{ local @_ = (1,0,13); my $imax=0; foreach(@_){my$ff = $0 .. !$0; $imax = $ff-1 if $_ > $_[$imax]}; $imax;}, 2, "pryrt: do(1,0,13)" ); is( do{ local @_ = (1,0,13,4); my $imax=0; foreach(@_){my$ff = $0 .. !$0; $imax = $ff-1 if $_ > $_[$imax]}; $imax;}, 2, "pryrt: do(1,0,13,4)" ); done_testing();