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.
I was going to tell you that
Data::Dumper
can help you sort out Perl data structures, butData::Dumper
can also tell you about your first problem:Here's what happens when you sign open-curly + close-curly ( '{}' ) to a hash:
Here's the output:
What you did is you assigned the stringified hash reference as a key to the list element that comes after it. implied
So Perl assigned it a value of
undef
.When you assign to a hash, you assign a list. A literal empty hash is not a list, it's a hash reference. What you wanted to do for an empty hash--and what is totally unnecessary--is this:
And that's unnecessary because it is exactly the same thing as:
You're declaring a hash, and that statement fully identifies it as a hash. And Perl is not going to guess what you want in it, so it's an empty hash from the get-go.
Finally, my advice on using
Data::Dumper
. If you started your hash off right, and did the following:What you see is:
So, you can better see how it's all getting stored, and what levels you have to deference and how to get the information you want out of it.
By the way,
Data::Dumper
delivers 100% runnable Perl code, and shows you how you can specify the same structure as a literal. One caveat, you would have to declare the variable first, usingstrict
(which you should always use anyway).You update @ports properly.
Your print statement accesses $storage[1] (reference to @ports) in wrong way.
You may use syntax you have used in
push
.