Kelly22 has asked for the wisdom of the Perl Monks concerning the following question:

My problem is that \@channelsSegments and \@channelsTime end up with the same data, which is the data from both the m/^($Channel)Time=/ and m/^$_=/) conditions. The conditions work correctly and I know the problem is at the push but I dont understand whats going wrong.
foreach (@channels){ $Channel=$_; if ($Line =~ m/^$_=/){ my @channelsSegmentsSplit=split(/=/,$Line); foreach (split(/\,/,$channelsSegmentsSplit[1])) +{ print "Segment\n"; print "$_ \n"; push @{$channelsSegments[$Count]}, $_; } } elsif($Line =~ m/^($Channel)Time=/){ my @channelsSegmentsTimeSplit=split(/=/,$Line); foreach (split(/\,/,$channelsSegmentsTimeSplit[1] +)){ print "Time\n"; print "$_ \n"; push @{$channelsTime[$Count]}, $_; } } $Count++; } } return (\@channels, \@channelsSegments, \@channelsTime, $readInterv +al);

Replies are listed 'Best First'.
Re: Multidimensional Array Trouble
by SuicideJunkie (Vicar) on Jun 04, 2012 at 16:52 UTC

    The first thing to do with complex data structures is to print them out with use Data::Dumper; print Dumper $mysteriousStructure;

    The symptom of getting the same thing on two branches is often a problem of storing multiple references to the same old array or hash.

    my @array; my %hash; push @array, 'foo'; $hash{ref1} = \@array; push @array, 'bar'; $hash{ref2} = \@array;

    At that point, both entries in the hash point to the same array, and that array contains both 'foo' and 'bar'.