Leser: 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#!/usr/bin/perl -w use strict; use warnings; use diagnostics; my %hash = ( 0=>'nul', 1=>'foo', 2=>'bar', 3=>'max', 4=>'mor', 5=>'zig', ); foreach my $nr ( sort {$a <=> $b} grep {$_ > 1} keys %hash ) { if ($nr == 3) { %hash = ( # diese Neudefinition entspricht einer Löschung von key '3/max' # zur Laufzeit der Schleife mit neuer Nummerierung der nachfolgenden Keys # Warum kommt die Schleife aus dem Tritt? 0=>'nul', 1=>'foo', 2=>'bar', 3=>'mor', 4=>'zig', ); $nr --; next; } print $nr . ' => ' . $hash{$nr} . "\n"; } __AUSGABE__ 2 => bar 4 => zig Use of uninitialized value in concatenation (.) or string at test14.pl line 34 (#1) (W uninitialized) An undefined value was used as if it were already defined. It was interpreted as a "" or a 0, but maybe it was a mistake. To suppress this warning assign a defined value to your variables. To help you figure out what was undefined, perl will try to tell you the name of the variable (if any) that was undefined. In some cases it cannot do this, so it also tells you what operation you used the undefined value in. Note, however, that perl optimizes your program and the operation displayed in the warning may not necessarily appear literally in your program. For example, "that $foo" is usually optimized into "that " . $foo, and the warning will refer to the concatenation (.) operator, even though there is no . in your program. 5 =>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#!/usr/bin/perl use strict; use warnings; my %hash = ( 0=>'nul', 1=>'foo', 2=>'bar', 3=>'max', 4=>'mor', 5=>'zig', ); my @keys = sort {$a <=> $b} grep {$_ > 1} keys %hash; my $i = 0; my $flag = 1; # damit der Abzweig nur einmal betreten wird while ( $i < @keys ) { if ( $flag && $keys[$i] == 3 ) { $flag = 0; %hash = ( 0=>'nul', 1=>'foo', 2=>'bar', 3=>'mor', 4=>'zig', ); @keys = sort { $a <=> $b } grep { $_ > 1 } keys %hash; $i--; next; } # if print "$keys[$i] => $hash{$keys[$i]}\n"; } continue { $i++ }
2010-08-11T06:32:43 biancaIch wäre nicht drauf gekommen, ein Array aus den Keys zu machen.
2010-08-07T22:19:29 MatthiasWwäre es hier vielleicht angebracht ein Array anstelle des Hashes zu verwenden