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' ] };
Bill

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
    > push an array

    push @results, @array; extends @results by the elements of @array, it doesn't "push an array", as all the elements of an array must be scalars. You can push an array reference, though:

    push @results, \@array;

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      Hello!

      Thanx to all. I will try to give a running code.

      With the dumper there is the problem, that are some hundret values.
      ydata => $fnfeld[$ploty[$ii]]->{'fn'},
      This call to gnuplot is working.
      But this not:
      push @{$plotx{$ii}}, $fnfeld[$xfeld[$i]]->{'fn'}; . . . xdata => $plotx{$ii},
      It is empty.
      I will try to create a short standalone code.

      Regards buchi

      Hello!

      >push @results, @array; extends @results by the elements of @array, it doesn't "push an array", as all the elements of an array must be scalars. You can push an array reference, though:

      yes, thanks, so to getting it keep as an array, I push it to a hash.

      e.g.
      my %plotx; my $i; my @xfix; push @{$plotx{$i}}, @xfix;
      Regards, buchi
        Hello!

        Putting the whole data, it is to much data. I think, I will resign the fight at this construction area and will make a workaround, that I will print out the data from the array, which contains a hash, which contains an array element by element and the read it into the new array, which is part of a hash.

        Regards, buchi