1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#!/usr/bin/env perl use warnings; use strict; use 5.10.0; use Term::Choose qw(choose); my @array = ( 'a' .. 'z' ); my @new; for my $i ( 0 .. $#array ) { my $choice = choose( [ 'ok', 'ans Ende', undef ], { prompt => "<$array[$i]>", undef => 'Exit' } ); exit if not defined $choice; if ( $choice eq 'ans Ende' ) { push @array, splice( @array, $i, 1 ); next; } push @new, $array[$i]; say "@new"; }
QuoteIf any part of LIST is an array, "foreach" will get very confused if you add or remove
elements within the loop body, for example with "splice". So don't do that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
while ( @array ) { my $el = shift @array; my $choice = choose( [ 'ok', 'ans Ende', undef ], { prompt => "<$el>", undef => 'Exit' } ); exit if not defined $choice; if ( $choice eq 'ans Ende' ) { push @array, $el; next; } push @new, $el; say "@new"; }