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:
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.
In here I need the check for each file and for each checkno for this file:
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.
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:
I cannot think of a way to create this hash?
Can you please help me?
Here is my example:
I would like to hash with this structure:
report
fileTobechecked
fileNo=$fileNo
checkResult=$checkResult
textcolor=$textcolor
check=$check
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;
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;
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;
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"
I cannot think of a way to create this hash?
Can you please help me?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
i thought this question was about Hoes :(
ASKER
My typo:
How to create multidimensional hash in Perl?
How to create multidimensional hash in Perl?
ASKER
@ozo: Can you please take look at this follow up question?
https://www.experts-exchange.com/questions/27837179/How-to-read-all-the-elements-of-a-multi-dimensional-hash-in-Perl.html
Thanks,
https://www.experts-exchange.com/questions/27837179/How-to-read-all-the-elements-of-a-multi-dimensional-hash-in-Perl.html
Thanks,