• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 658
  • Last Modified:

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:


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) {
		$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) {
            #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?
  • 2
1 Solution
instead of
$report{ $fileTobeChecked }{ $checkno } = $check;
$report{ $fileTobeChecked }{ $checkno }{check} = $check;
Ess KayEntrapenuerCommented:
i thought this question was about Hoes :(
TolgarAuthor Commented:
My typo:

How to create multidimensional hash in Perl?
TolgarAuthor Commented:
@ozo: Can you please take look at this follow up question?


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now