in reply to question to a complex structure of hash/array
When $xfeld$I is equal to either an 'x' or a 'y', You push an array into the output. Otherwise you push a scalar. Your final prints expect the array. Your first print section shows that you stored the scalar when $i was equal to 0,1,4,5,7, and 8. Your final section would fail for all of these, but you do not try to print for $i equal to 0, 1, or 4. It does fail when $i is equal to 5, 7, and 8. Your print works as expected for the other cases (2, 3, 6, and 9).
I cannot suggest a solution, because you have not shown us any data.
UPDATE: Added code which duplicates the posted output. Removed irrelevant code from and added initialization to originally posted code. A dump of the output hash %plotx reveals what happens.
use strict; use warnings; use Data::Dumper; # Data infered from posted output. my @xfeld = qw(0 0 y y 0 5 x 5 5 y); my @xfix = qw( UNKNOWN_x_0 UNKNOWN_x_1 UNKNOWN_x_2 3.640250E-4 ); my @yfix = qw( UNKNOWN_y_0 UNKNOWN_y_1 UNKNOWN_y_2 3.640000E-4 ); my @fnfeld = ( {fn => 'UNKNOWN_n_0',}, {fn => 'UNKNOWN_n_1',}, {fn => 'UNKNOWN_n_2',}, {fn => 'UNKNOWN_n_3',}, {fn => 'UNKNOWN_n_4',}, {fn => 'UNKNOWN_n_5',}, {fn => 'UNKNOWN_n_6',}, {fn => 'UNKNOWN_n_7',}, {fn => 'UNKNOWN_n_8',}, {fn => 'UNKNOWN_n_9',}, ); my @yfeld; $#yfeld = $#xfeld; my %plotx; my $ii = -1; for (my $i = 0; $i <= $#yfeld; $i++) { $ii = $ii + 1; print "$i $ii "; if ($xfeld[$i] eq 'x') { push @{$plotx{$ii}}, @xfix; print "x\n"; } elsif ($xfeld[$i] eq 'y') { push @{$plotx{$ii}}, @yfix; print "y\n"; } else { push @{$plotx{$ii}}, $fnfeld[$xfeld[$i]]->{'fn'}; # The Previous statement stores only one value. # The failing prints expects at least four. print "$xfeld[$i]\n"; } } print "\n"; print "2 $plotx{2}[3]\n"; print "3 $plotx{3}[3]\n"; print "5 $plotx{5}[3]\n"; # Array element does not exist print "6 $plotx{6}[3]\n"; print "7 $plotx{7}[3]\n"; # Array element does not exist print "8 $plotx{8}[3]\n"; # Array element does not exist print "9 $plotx{9}[3]\n"; print Dumper(\%plotx); OUTPUT: 0 0 0 1 1 0 2 2 y 3 3 y 4 4 0 5 5 5 6 6 x 7 7 5 8 8 5 9 9 y 2 3.640000E-4 3 3.640000E-4 Use of uninitialized value in concatenation (.) or string at buchi2.pl + line 41. 5 6 3.640250E-4 Use of uninitialized value in concatenation (.) or string at buchi2.pl + line 43. 7 Use of uninitialized value in concatenation (.) or string at buchi2.pl + line 44. 8 9 3.640000E-4 $VAR1 = { '1' => [ 'UNKNOWN_n_0' ], '8' => [ 'UNKNOWN_n_5' ], '6' => [ 'UNKNOWN_x_0', 'UNKNOWN_x_1', 'UNKNOWN_x_2', '3.640250E-4' ], '3' => [ 'UNKNOWN_y_0', 'UNKNOWN_y_1', 'UNKNOWN_y_2', '3.640000E-4' ], '2' => [ 'UNKNOWN_y_0', 'UNKNOWN_y_1', 'UNKNOWN_y_2', '3.640000E-4' ], '9' => [ 'UNKNOWN_y_0', 'UNKNOWN_y_1', 'UNKNOWN_y_2', '3.640000E-4' ], '7' => [ 'UNKNOWN_n_5' ], '5' => [ 'UNKNOWN_n_5' ], '0' => [ 'UNKNOWN_n_0' ], '4' => [ 'UNKNOWN_n_0' ] };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: question to a complex structure of hash/array
by choroba (Cardinal) on Aug 08, 2017 at 15:55 UTC | |
by buchi2 (Acolyte) on Aug 09, 2017 at 04:28 UTC | |
by buchi2 (Acolyte) on Aug 09, 2017 at 07:55 UTC | |
by buchi2 (Acolyte) on Aug 09, 2017 at 08:10 UTC |