Perhaps I have made this more complicated than I need it to be but I am currently trying to store an array that contains, among other things, an array inside a hash in Perl.
i.e. hash -> array -> array
use strict;
my %DEVICE_INFORMATION = {}; #global hash
sub someFunction() {
my $key = 'name';
my @storage = ();
#assume file was properly opened here for the foreach-loop
foreach my $line (<DATA>) {
if(conditional) {
my @ports = ();
$storage[0] = 'banana';
$storage[1] = \@ports;
$storage[2] = '0';
$DEVICE_INFORMATION{$key} = \@storage;
}
elsif(conditional) {
push @{$DEVICE_INFORMATION{$key}[1]}, 5;
}
}#end foreach
} #end someFunction
This is a simplified version of the code I am writing. I have a subroutine that I call in the main. It parses a very specifically designed file. That file guarantees that the if statement fires before subsequent elsif statement.
I think the push call in the elsif statement is not working properly - i.e. 5 is not being stored in the @ports array that should exist in the @storage array that should be returned when I hash the key into DEVICE_INFORMATION.
In the main I try and print out each element of the @storage array to check that things are running smoothly.
#main execution
&someFunction();
print $DEVICE_INFORMATION{'name'}[0];
print $DEVICE_INFORMATION{'name'}[1];
print $DEVICE_INFORMATION{'name'}[2];
The output for this ends up being... banana ARRAY(blahblah) 0
If I change the print statement for the middle call to:
print @{$DEVICE_INFORMATION{'name'}[1]};
Or to:
print @{$DEVICE_INFORMATION{'name'}[1]}[0];
The output changes to banana [blankspace] 0
Please advise on how I can properly update the @ports array while it is stored inside the @storage array that has been hash'd into DEVICE_INFORMATION and then how I can access the elements of @ports. Many thanks!
P.S. I apologize for the length of this post. It is my first question on stackoverflow.