I'm getting the error Can't use an undefined value as an ARRAY reference
in a Perl script. Below is a highly simplified version.
Basically I have setup a hash of arrays where some of them might be empty (B in this case). It works just fine if I DON'T sort the data array. I've tried adding a conditional to test if that particular array exists, but it doesn't like that in this setup. It would not be easy to sort the array as it's populated (unlike in this example).
use strict;
use warnings;
my @list = ('A', 'B', 'C');
my %data_for;
$data_for{'A'} = ['apple', 'astronaut', 'acorn'];
$data_for{'C'} = ['car', 'cook', 'candy'];
# Creates error
foreach my $letter (@list) {
print "$letter: ";
foreach my $item ( sort @{$data_for{$letter}}) {
print "$item, ";
}
print "\n";
}
This is the output I want (seems obvious, but eh):
A: acorn, apple, astronaut,
B:
C: candy, car, cook,
As a strange aside, if I print the working version (without the sort) first, the second version with the sort works without an error. I do not understand this, but I may be able to use that as a work around.