I have an array like so:
@switch_ports = ()
and then want to add 50 instances of this hash, to the switch_ports array.
%port = (data1 => 0, data2 => 0, changed => 0)
However, if I push my hash to the array:
push(@switch_ports, %port)
and I do print @switch_ports, I just see:
data10data20changed0
so it just seems to be adding them to the array, (joining them)
and if I try and loop the array and print the keys, it also fails.
-
Can you store a hash in an array?
-
Can you have an array of hashes?
I’m trying to get this:
switchports
0
data1
data2
changed
1
data1
....
thus:
foreach $port (@switchport) {
print $port['data1']
}
would return all of the data1 for all of the hashes in the array.
In Perl, array and hash members must be a single value. Before Perl 5.0, there was no (easy) way to do what you want.
However, in Perl 5 you can now use a reference to your hash. A reference is simply the memory location where the item is being stored. To get a reference, you put a backslash in front of the variable:
Thus:
And, you don’t have to create
$port_ref:To get the actual value of the reference, simply put the symbol back on front:
Another shortcut:
Or, even shorter, dereferencing as you go along:
And a little syntactic sweetener. It means the same, but is easier to read:
Take a look at Perldoc’s perlreftut and perlref. The first one is a tutorial.