If I understand correctly what you're trying to do, then @Gever's answer should do the trick. Here's an alternative implementation using regexes rather than unpack:
use 5.010;
use strict;
use warnings;
my @file = glob("/path/window/*_testing_42.csv");
foreach my $file (@file) {
my($name) = $file =~ /(\w+)_testing_42/;
my @code = $name =~ /(...)/g;
say 'Parts found: ', scalar(@code); # Parts found: 4
say $code[0]; # G43
say $code[1]; # B76
say $code[2]; # P90
say $code[3]; # T45
}
I used an array rather than a hash, because that makes more sense to me, but if you really want a hash, you could do it like this:
foreach my $file (@file) {
my($name) = $file =~ /(\w+)_testing_42/;
my %hash;
@hash{'first', 'second', 'third', 'fourth'} = $name =~ /(...)/g;
say $hash{first}; # G43
say $hash{second}; # B76
say $hash{third}; # P90
say $hash{fourth}; # T45
}
In this line:
my($name) = $file =~ /(\w+)_testing_42/;
The parentheses around $name are important because they force the match to be evaluated in list context, which returns the parts of the regex that were captured in the (\w+). Without the parentheses, the value 1 would be assigned to $name because there was 1 match.
The syntax for assigning a list of values to a series of keys in a hash (called a 'hash slice') is somewhat confusing. Perl knows we're assigning values into %hash because of the { after the variable name, but we put a @ before the variable name to indicate we're assigning multiple values to a hash slice. Using a $ before the variable name would indicate we're assigning to a single value in the hash.
The other thing I changed from your code is that I declared %hash inside the loop. Which means that you can only refer to it inside the loop. If you declare it outside the loop, one set of values will persist after each matching filename has been processed, but the hash might contain values from different filenames depending on how many fields were present on the last iterations.
@files, or do you want to open the files, read lines of data from them and match against those lines? It would be helpful if you could provide examples of the filenames if that's what you're matching, or examples of file contents.'G43B76P90T45'into "letter-numbers" patterns (the goal I assume from the question):my @parts = 'G43B76P90T45' =~ /([a-zA-Z][0-9]+)/g;But the code shown in the question is "Illegal" with many basic syntax errors and I can't tell whether the above will actually work in your real code. Just once more: please show us your actual code :)