Hoe to create multidimensional hash in Perl?

Posted on 2012-08-21
Last Modified: 2012-08-21
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?
Question by:Tolgar
    LVL 84

    Accepted Solution

    instead of
    $report{ $fileTobeChecked }{ $checkno } = $check;
    $report{ $fileTobeChecked }{ $checkno }{check} = $check;
    LVL 15

    Expert Comment

    by:Ess Kay
    i thought this question was about Hoes :(

    Author Comment

    My typo:

    How to create multidimensional hash in Perl?

    Author Comment

    @ozo: Can you please take look at this follow up question?


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
    There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now