I'm trying to build a hash table using build that that read a list of files, and store each value to the hash, something like this: - open directory and list them in array - then open each file and get some value from each file and put them into has table with file name, total, pass, and fail to the hash table
#!/usr/bin/perl
use strict;
my $dir = "../result";
opendir(DIR, $dir) or die $!;
my %result = ();
while (my $file = readdir(DIR)) {
# We only want files
next unless (-f "$dir/$file");
# do something here and get some value from each file
$total = $worksheet->get_cell(0,1);
$pass = $worksheet->get_cell(1,1);
$fail = $worksheet->get_cell(2,1);
# Print the cell value when not blank
$total = $total->value();
$pass = $pass->value();
$fail = $fail->value();
%result = (
"name" => "$file",
"total" => "$total",
"pass" => "$pass",
"fail" => "$fail"
);
}
foreach my $key (keys %result) {
print "Key: $key, Value: $result{$key}\n";
}
When I run it through the forloop I get only last entry or last file on directory, how do I add and build hash that keeps track of all files with keys & value mentioned above.. thanks in advance..
File::Findmoduleuse strictat the top of your program to avoid a telling off? It's good to have it there, but you also need to declare your variables. As it stands your program won't compile.