Thread Code Review: Innerhalb einer for Schleife das verwendete Array verändern (5 answers)
Opened by Kuerbis at 2013-06-30 17:21

Kuerbis
 2013-06-30 17:21
#168678 #168678
User since
2011-03-20
955 Artikel
BenutzerIn
[default_avatar]
Hallo!

In diesem Code verändere ich array innerhalb der for Schleife. Fällt so etwas in die Kategorie
[x] Kann man so machen.
oder in die Kategorie
[x] Sollte man nicht tun.
?

Code (perl): (dl )
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";  
}

View full thread Code Review: Innerhalb einer for Schleife das verwendete Array verändern