1

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..

2
  • You may want to look at File::Find module Commented Nov 1, 2013 at 17:36
  • Did you put use strict at 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. Commented Nov 1, 2013 at 19:20

2 Answers 2

1

You only get the last value because you are overwriting the value of %result each time through the loop.

It sounds like you want an array of hashes rather than a single hash. How about something like this:

my @results;
while (my $file = readdir(DIR)) {
    ...
    my %file_result = (
        "name"  => "$file", 
        "total" => "$total", 
        "pass"  => "$pass",
        "fail"  => "$fail"
    );

    push @results, \%file_result;
}
Sign up to request clarification or add additional context in comments.

1 Comment

@user2912312, make sure you check the green checkmark next to the answer to indicate that it is accepted.
1

Store what you want IN the hash:

Instead of:

   %result = (
              "name"  => "$file", 
              "total" => "$total", 
              "pass"  => "$pass",
              "fail"  => "$fail"
   );

which replaces the entire hash every time, try something like:

$result{$file} = "$total $pass $fail";

In which case, the keys of the hash will be the file names, and the values concatenated strings of the other values.

You can also make the elements of the hash something more complicated, like an array holding those three values, or whatever you might need.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.