Is it possible to create different named arrays while looping in Perl? What I need to do is open some files from @ARGV and put their data in separate arrays in one loop, for example: 1.txt in @first_array, 2.txt in @second_array and etc. Is it possible and if yes, what would be the best approach?
EDIT:
I think I'm getting closer, since Data::Dumper shows a correct structure of what I need, but it doesn't show the values of the files, instead, it shows this:
$VAR1 = {
'skai.txt' => [
\*{'::$fh'},
$VAR1->{'skai.txt'}[0],
$VAR1->{'skai.txt'}[0],
$VAR1->{'skai.txt'}[0]
],
'numb.txt' => [
\*{'::$fh'},
$VAR1->{'numb.txt'}[0],
$VAR1->{'numb.txt'}[0],
$VAR1->{'numb.txt'}[0]
]
};
Each file consists of 4 numbers. My code looks like this:
use strict;
use warnings;
use Data::Dumper;
my $data = {};
foreach my $arg(@ARGV){
if(open(my $fh, $arg)){
$data->{$arg}=[];
while(<$fh>){
chomp;
push @{$data->{$arg}}, $fh;
}
close($fh);
}
}
print Dumper $data;
What is the meaning of \*{'::$fh'}?
first_array,second_array, etc? Or do you just want to make sure each file gets loaded into a separate array?