my @msgs = ('msg1', 'msg2', ..., 'msg6');
my @vals = ( [ @val1 ], [ @val2 ], ..., [ @val6 ] );
See also the FAQ How can I use a variable as a variable name?
As the answer to the FAQ notes, if the variables are not indexed by an integer, you can use a hash table:
By using symbolic references, you are just using the package's symbol-table hash (like
%main::
) instead of a user-defined hash. The solution is to use your own hash or a real reference instead.$USER_VARS{"fred"} = 23; my $varname = "fred"; $USER_VARS{$varname}++; # not $$varname++
You should read the entire FAQ list at least once a year.
Update: I purposefully left symbolic references out of my answer because they are unnecessary and likely very harmful in the context of your question. For more information, see Why it's stupid to 'use a variable as a variable name'?, part 2 and part 3 by mjd.
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?