I have an array of strings that I want to use in a loop and then call later as names of a hash. I wrote a test file to play around with it, but I can't get it to work no matter how hard I am trying. It keeps giving me "can't use string as a hash ref while strict refs is in use". I can turn off strict refs, but then the code simply skips those lines and doesn't do what I want. Is there any way to evaluate a variable that contains a string and then pipe it to the name of a hash?
Example code:
#usr/bin/perl
use strict;
my %combined = ();
my %highmut = ();
$combined{'a'} = 1;
$combined{'b'} = 2;
$highmut{'c'} = 3;
$highmut{'d'} = 4;
my @counts = ('combined', 'highmut');
foreach my $count (@counts) {
my $file = $count . '.txt';
open(my $random, ">>", $file);
foreach my $key (keys %{$count}) {
print $random "key: $key \t %{$count{$key}} \n";
close($random);
}
}
Specifically, the issues are with %{$count} and %{$count{$key}} in the last few lines.
What I want is to evaluate $count (as say combined) and then feed it to use as hash name (%combined).
Is there any way to do this?
Thanks