Link to home
Start Free TrialLog in
Avatar of Tolgar
Tolgar

asked on

Hoe to create multidimensional hash in Perl?

How can I create a multi-dimensional hash in Perl?

Here is my example:

I would like to hash with this structure:

report 
          fileTobechecked
                                     fileNo=$fileNo
                                     checkResult=$checkResult
                                     textcolor=$textcolor
                                     check=$check

Open in new window



So, report will be the hash. fileNo, checkResult,  textcolor and check are the keys and their values change in a for loop.

Let me show you the part of my code:

In here I need a fileNo for each fileTobeChecked. The following code works fine actually.
    
my %report;

foreach my $fileTobeChecked (@allFilesTobeChecked) {
        $fileNo++;
		$report{ $fileTobeChecked }{ 'fileNo' } = $fileNo;

Open in new window


In here I need the check for each file and for each checkno for this file:

        foreach my $check (@Checks) {
            $checkno++;
            #removing new line character
            $check =~ s/\n//g;
			$report{ $fileTobeChecked }{ $checkno } = $check;

Open in new window


                                   
And in here, I need CheckResult for each file of for each checkno. But this has a problem. Because $report{ $fileTobeChecked }{ $checkno } is no longer a hash. It becomes a string.

$report{ $fileTobeChecked }{ $checkno }{ 'checkResult'} = $CheckResult;   

Open in new window

                               

But, the way I create these hashes, it does not work. Because, for the last example code, $report{ $fileTobeChecked }{ $checkno } is not a hash. it becomes a string. Therefore, I get  this error:

Can't use string ("  WARNING: The file type does no") as a HASH ref while "strict refs"

Open in new window


I cannot think of a way to create this hash?

Can you please help me?
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i thought this question was about Hoes :(
Avatar of Tolgar
Tolgar

ASKER

My typo:

How to create multidimensional hash in Perl?