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 55 56 57
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $ranges = { 1282 => [ 25, 40, 5, undef ], 1283 => [ 300, 400, 25, 1282 ], 1284 => [ 20, 30, 5, undef ], 1285 => [ 250, 260, 10, 1284 ], 1286 => [ 25, 30, 5, undef ], 1287 => [ "0.003", "0.005", "0.001", 1286 ], }; for my $range (keys %$ranges) { my $related_r = @{ $ranges->{$range} }[3]; if ( defined $related_r ) { my @merge_range; @merge_range = ( @{$ranges->{$range}}[0..2], @{$ranges->{$related_r}}[0..2] ); delete $ranges->{$related_r}; $ranges->{$range} = \@merge_range; } } print "EDITED RANGES:\n"; print Dumper $ranges;
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
EDITED RANGES:
$VAR1 = {
'1283' => [
300,
400,
25,
25,
40,
5
],
'1287' => [
'0.003',
'0.005',
'0.001',
25,
30,
5
],
'1282' => [],
'1285' => [
250,
260,
10,
20,
30,
5
],
'1284' => []
};
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
perl code_191508_1.pl
EDITED RANGES:
$VAR1 = {
'1282' => [],
'1283' => [
300,
400,
25,
25,
40,
5
],
'1285' => [
250,
260,
10,
20,
30,
5
],
'1286' => [],
'1287' => [
'0.003',
'0.005',
'0.001',
25,
30,
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
$ perl code_191508_1.pl
EDITED RANGES:
$VAR1 = {
'1282' => [],
'1283' => [
300,
400,
25,
25,
40,
5
],
'1285' => [
250,
260,
10,
20,
30,
5
],
'1287' => [
'0.003',
'0.005',
'0.001',
25,
30,
5
]
};
1 2 3 4
for my $range (keys %$ranges) { next unless exists $ranges->{$range} my $related_r = @{ $ranges->{$range} }[3]; # ...
2020-03-13T21:44:35 styx-ccIch werde künftig mit exists prüfen.
2020-03-14T09:31:57 RaubtierGenerell würde ich immer dringend davon abraten (!) ein einer Loop über ein Objekt dieses auch (strukturell) zu verändern.
2020-03-14T14:06:18 RaubtierEben. Man darf durchaus drauf vertrauen, was in perlfunc keys steht:(gut, ist vielleicht nicht das beste Argument, wo ich sonst immer sage, dass man ruhig alle Features einer Sprache nutzen soll).
Quoteund weiter unten:Called in list context, returns a list consisting of all the keys of the named hash...
QuoteDie Liste ist also robust gegenüber allem, was mit dem Hash hintherher passiert.The returned values are copies of the original keys in the hash...
QuoteTo avoid these problems, use a "foreach" loop rather than "while"-"each".
Quotefrag ich einfach mal was ich falsch mache.
2020-03-14T08:20:25 rostiDeine Datenstruktur ist unzweckmäßig.
2020-03-14T15:16:45 hlubenow2020-03-14T08:20:25 rostiDeine Datenstruktur ist unzweckmäßig.
Ich denke, wir kennen die Daten, um die es geht, nicht genau genug, um so eine Aussage zu treffen.
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
#!/usr/bin/perl use warnings; use strict; use Data::Dumper; use Clone qw(clone); my $ranges = { 1282 => [25, 40, 5, undef], 1283 => [300, 400, 25, 1282], 1284 => [20, 30, 5, undef], 1285 => [250, 260, 10, 1284], 1286 => [25, 30, 5, undef], 1287 => ["0.003", "0.005", "0.001", 1286]}; my $ranges_clone = clone($ranges); for my $range (keys %$ranges) { my $related_r = @{ $ranges->{$range} }[3]; if ( defined $related_r ) { my @merge_range; @merge_range = ( @{$ranges->{$range}}[0..2], @{$ranges->{$related_r}}[0..2] ); delete $ranges_clone->{$related_r}; $ranges_clone->{$range} = \@merge_range; } } print "EDITED RANGES:\n"; print Dumper $ranges_clone;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $ranges = { 1282 => [ 25, 40, 5, undef ], 1283 => [ 300, 400, 25, 1282 ], 1284 => [ 20, 30, 5, undef ], 1285 => [ 250, 260, 10, 1284 ], 1286 => [ 25, 30, 5, undef ], 1287 => [ 0.003, 0.005, 0.001, 1286 ], }; my %merged = map { $_ => [@{$ranges->{$_}}[0..2], @{$ranges->{$ranges->{$_}[3]}}[0..2]] } grep { defined $ranges->{$_}->[3] } keys %$ranges; print "EDITED RANGES:\n"; print Dumper \%merged;