哈希散列值 [英] Hash of hashes perl

查看:138
本文介绍了哈希散列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是我正在尝试创建散列哈希,然后为每个哈希添加更多元素。我正在阅读一个文件,并且即时匹配各种项目,我想将它们存储在不同的子哈希中 - ('类别','项目','描述','分数')。后来我需要将它们存储在一个CSV文件中,但目前我只是试图打印它们,但是我收到了很多错误。
这是我的代码:

  use strict; 
使用警告;

my%MacroA =('Category','Item','Description','Score');

open(my $ file,'<','Layout.txt')或die $!

while(my $ line =< $ file>){

if($ line =〜/ \b(layout)\b,/){
$ MacroA {Category} ++;

elsif($ line =〜/,([\ w \s] +?),/){
$ MacroA {Item} ++;
}

elsif($ line =〜/,\b(\w+?.\d+?)\b,/g){
$ MacroA {应将描述} ++;
}
elsif($ line =〜/,\b(pass | fail)\b /){
$ MacroA {Score} ++;
}
}

close $ file;

foreach我的$类别(键%{$ MacroA {$类别}}){
打印$类别,\ n;
}
foreach my $ items(keys%{$ MacroA {$ Item}}){
print $ items,\\\
;
}
foreach my $ descriptions(keys%{$ MacroA {$ Description}}){
print $ descriptions,\\\
;
}
foreach我的$分数(按键%{$ MacroA {$ Score}}){
打印$分数,\\\
;
}

以及我得到的错误:

 全局符号$ Category需要在MacroA.pl第29行显式包名。
全局符号$ Item需要在MacroA.pl行处显式包名32.
全局符号$ Description需要在MacroA.pl第35行显式包名称。
全局符号$ Score需要在MacroA.pl第38行显式包名。
执行由于编译错误,MacroA.pl中止。 。 。


解决方案

您的问题仅代表一些基本的错误理解,初学者结束了挣扎。评论是完全正确的:当这种事情发生时,阅读你的错误,并尝试使用一些很好的教程或书籍来理解它们。我会尽力帮助你,尽管我不太清楚你正在努力达到什么目的。



你说你想创建一个哈希散列但通过代码的外观,您只需创建一个计数哈希值,您可以在其中记录您正在阅读的文件中每个第一级密钥(类别,项目等)的次数。这不是散列的散列 - %MacroA 目前只包含一个散列级别。



第一个问题是,您正试图使用​​您想要使用的键列表来定义您的哈希。

  my%MacroA =('Category','Item','Description','Score'); 

你在这里实际做的是试图传递一个数组你的钥匙。如果你给一个数组散列,它会将第一个元素解释为其键,第二个解释为该键的值,第三个解释为第二个键,依此类推。所以你最终会得到这个

 %MacroA =(
Category => Item,
Description => Score

要初始化只有一级密钥的哈希,可以给每个键的初始计数为0:

  my%MacroA =('Category',0,'Item',0 ,'Description',0,'Score',0); 

通常用箭头符号 => 使事情更加清楚b / b
$ b

  my%MacroA =('Category'=> 0,'Item'=> 0,'Description'=> 0,'Score',0); 

要初始化第二级密钥的散列,您可以这样做(正如您在评论)

  my%MacroA =('Category'=> {},'Item'=> {},'描述'=> {},'Score',{}); 

事实上,你甚至不需要初始化你的散列值为空值的键,因为perl会当你第一次使用它时,使用autovivification来初始化一个空的键。因此,你真正必须做的就是像初始化hash变量那样

  my%MacroA; 

我会在这里猜一猜,假设你的分类,描述等等是你的第一个等级然后无论你在文件中找到什么作为第二级密钥。我想你要计算第二级密钥出现的次数。



目前,您的散列正在跟踪Category,Item ...键,因为您正在增加使用这些键存储的值:

  $ MacroA {Category} ++; 

因为您的正则表达式匹配将返回匹配到标量变量 $ 1 ,您可以使用它来增加第二级散列,如下所示:

  $ MacroA {类别} - > {$ 1} ++; 

最后,在打印出您的计数时,您会犯一个简单的错误, :

  $ MacroA {$ Category} 

你可能打算为每个第一关键字使用一个字符串:

  $ {MacroA '分类'}; 

Perl认为您正在尝试使用一个名为 $ Category ,因此会引发错误。



最后,如果您打印 Dumper 格式化您的数据结构的日志在代码中的某些点。只需使用代码

 使用Data :: Dumper; 
print Dumper(\%MacroA);

可以查看%MacroA 在那个时间点。

希望这可以帮助你奋斗!


The problem is that I am trying to create a hash of hashes and then add more elements to each hash. I am reading from a file and im matching various items and I want to store them in a different subhash - ('Category', 'Item', 'Description', 'Score'). Later I will need to store them in a CSV file but at the moment I am just trying to print them, but I am getting a lot of errors. Here is my code:

use strict;
use warnings;

my %MacroA = ('Category', 'Item', 'Description', 'Score');

open (my $file, '<', 'Layout.txt') or die $!;

while (my $line = <$file>) {

    if ($line  =~ /\b(layout)\b,/) {
        $MacroA{Category}++;
    }
    elsif ($line =~ /,([\w\s]+?),/) {
        $MacroA{Item}++;
    }

    elsif ($line =~ /,\b(\w+?.\d+?)\b,/g) {
        $MacroA{Desciption}++;
    }
    elsif ($line =~ /,\b(pass|fail)\b/) {
        $MacroA{Score}++;
    }
}

close $file;

foreach my $categories (keys %{ $MacroA{$Category}}) {
    print $categories, "\n";
}
foreach my $items (keys %{ $MacroA{$Item}}) {
    print $items, "\n";
}
foreach my $descriptions (keys %{ $MacroA{$Description}}) {
    print $descriptions, "\n";
}
foreach my $scores (keys %{ $MacroA{$Score}}) {
    print $scores, "\n";
}

and the errors i get:

Global symbol "$Category" requires explicit package name at MacroA.pl line 29.
Global symbol "$Item" requires explicit package name at MacroA.pl line 32.
Global symbol "$Description" requires explicit package name at MacroA.pl line 35.
Global symbol "$Score" requires explicit package name at MacroA.pl line 38.
Execution of MacroA.pl aborted due to compilation errors. . .

解决方案

Your issues simply represent a few basic missunderstandings that any programming beginner ends up struggling through. The comments are completely correct: when this sort of thing happens, read your errors and try to make sense of them using a few good tutorials or books. I'll have a go at helping you, although I don't quite know what you are trying to achieve.

You say you want to create a "hash of hashes" but by the looks of your code you are simply creating a counting hash, where you are keeping track of the number of times each of your first level "keys" (Category, Item, etc.) occurs within the file you are reading in. This isn't a hash of hashes as it stands - %MacroA consists of only one level of hashes at the moment.

The first issue is that you are trying to define your hash using a list of the keys you want to use.

my %MacroA = ('Category', 'Item', 'Description', 'Score');

What you are actually doing here is trying to pass an array of your keys. If you give a hash an array, it will interpret the first element as its key, the second as that key's value, the third as the second key, and so on. So you will end up with this

%MacroA = (
    Category => Item,
    Description => Score
)

To initialise the hash for just one level of keys, you could give each key an initial count of 0:

my %MacroA = ('Category', 0, 'Item', 0, 'Description', 0, 'Score', 0);

This is usually written with arrow symbols => to make things more clear

my %MacroA = ('Category' => 0, 'Item' => 0, 'Description' => 0, 'Score', 0);

To intialise the hash for a second level of keys you could do this (as you correctly suggested in the comments)

my %MacroA = ('Category' => {}, 'Item' => {}, 'Description' => {}, 'Score', {});

In fact, you don't even need to initialise your hash to keys with empty values because perl will use autovivification to initialise an empty key when you first use it. So all you really have to do is initialise the hash variable like so

my %MacroA;

I will take a guess here and assume you meant to have Category, Description etc. as your first level of keys and then whatever you find in your file as the second level of keys. I think you want to count the number of times the second level of keys occur.

At the moment, your hash is keeping track of Category, Item... keys because you are incrementing the values stored with these keys:

$MacroA{Category}++;

Because your regex matches will return whatever they matched into the scalar variable $1, you can use that to increment a second level of hashes like so:

$MacroA{Category}->{$1}++;

Lastly, when printing out your counts, you make the simple mistake of referencing your key strings with a variable:

$MacroA{$Category}

Where you probably meant to use a string for each of the first level keys:

$MacroA{'Category'};

Perl thinks you are trying to use an undefined variable called $Category and so throws the errors you are getting.

Finally, it will definitely help your understanding if you print Dumper formatted logs of your data structures at certain points in your code. Just use the code

use Data::Dumper;
print Dumper(\%MacroA);

at any point in your script to view the structure of %MacroA at that point in time.

Hopefully this helps you to struggle on!

这篇关于哈希散列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆