如何在脚本执行期间创建匿名哈希并将其添加到已知哈希?

发布于 2024-08-09 03:50:25 字数 1195 浏览 2 评论 0原文

我将尝试用一个例子来说明这一点。举一个哈希的哈希的常见示例:

my %HoH = (
    flintstones => {
        lead => "fred",
        pal  => "barney",
    },
    jetsons => {
        lead      => "george",
        wife      => "jane",
        "his boy" => "elroy",
    },
    simpsons => {
        lead => "homer",
        wife => "marge",
        kid  => "bart",
    },
);

出于我的目的,我希望能够向 %HOH 添加未命名或匿名哈希。在运行时之前我不需要(或能够)定义这些子哈希。我怎样才能用 Perl 完成这个任务?

我读过的所有内容(我已经读过 Perldocs 和 Google)似乎都显示了定义所有 sub-hahes(例如“flintstones”、“jetsons”和“simpsons”)的示例。

我正在做的是尝试构建一个父哈希,其中包含来自 CSV 文件的行的子哈希:

%TopHash = (
   %Line1 => {
      cell01 => $some_value1a;
      cell02 => $some_value2a;
      cell03 => $some_value3a;
   },
   %Line2 => {
      cell01 => $some_value1b;
      cell02 => $some_value2b;
      cell03 => $some_value3b;
   },
   %Line3 => {
      cell01 => $some_value1c;
      cell02 => $some_value2c;
      cell03 => $some_value3c;
   },
# etc
# etc
# etc

    );

我需要的“%LineX”哈希的数量直到运行时才知道(因为它们表示运行时读取的 CSV 中的行数)。

有什么想法吗?如果还不清楚的话...我仍在尝试了解 Perl 哈希值。

I'll attempt to illustrate this with an example. Take a common example of a Hash of Hashes:

my %HoH = (
    flintstones => {
        lead => "fred",
        pal  => "barney",
    },
    jetsons => {
        lead      => "george",
        wife      => "jane",
        "his boy" => "elroy",
    },
    simpsons => {
        lead => "homer",
        wife => "marge",
        kid  => "bart",
    },
);

For my purposes, I would like to be able to add an unnamed, or anonymous hashes to %HOH. I won't need (or be able to) define these sub-hashes until runtime. How can I accomplish this with Perl?

Everything I've read (and I have read through Perldocs and Google'd already) seems to show examples where all sub-hahes (e.g. "flintstones", "jetsons", and "simpsons") are defined.

What I am doing is attempting to build a parent Hash that will contain sub-hashes with rows from a CSV file:

%TopHash = (
   %Line1 => {
      cell01 => $some_value1a;
      cell02 => $some_value2a;
      cell03 => $some_value3a;
   },
   %Line2 => {
      cell01 => $some_value1b;
      cell02 => $some_value2b;
      cell03 => $some_value3b;
   },
   %Line3 => {
      cell01 => $some_value1c;
      cell02 => $some_value2c;
      cell03 => $some_value3c;
   },
# etc
# etc
# etc

    );

The number of "%LineX" hashes that I need is not known until runtime (because they represent the number of lines in a CSV that is read at runtime).

Any ideas? If it isn't clear already...I still am trying to wrap my head around Perl hashes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

櫻之舞 2024-08-16 03:50:25

要在运行时添加匿名哈希,请像分配普通哈希元素一样分配它:

$HoH{key} = { foo => 42 };

$HoH{key} = $hash_ref;

$HoH{key} = \%hash;

To add an anonymous hash at runtime, assign it as you would a normal hash element:

$HoH{key} = { foo => 42 };

or

$HoH{key} = $hash_ref;

or

$HoH{key} = \%hash;
未央 2024-08-16 03:50:25

首先,从正在解析的当前行创建散列

my %lineHash = (
    cell01 => $some_value1a,
    cell02 => $some_value1b,
    cell03 => $some_value1c
);

,或者直接创建对散列的引用,

my $lineHashRef = {
    cell01 => $some_value2a,
    cell02 => $some_value2b,
    cell03 => $some_value2c
};

然后将其添加到整个散列中,记住嵌套的 Perl 结构只包含对其他结构的引用。

$topHash{line1} = \%lineHash;
$topHash{line2} = $lineHashRef;

已更新
给定一个要解析的数据数组的循环的示例

my %topHash;
foreach my $i (0 .. $#data) {
    my %tempHash;
    // stuff here to parse $data[$i] and populate %tempHash
    $topHash{"line$i"} = \%tempHash;
}

First you create the hash from the current line you're parsing

my %lineHash = (
    cell01 => $some_value1a,
    cell02 => $some_value1b,
    cell03 => $some_value1c
);

or create a reference to a hash outright

my $lineHashRef = {
    cell01 => $some_value2a,
    cell02 => $some_value2b,
    cell03 => $some_value2c
};

Then you add it to your overall hash, remembering that nested perl structures just contain references to the other structures.

$topHash{line1} = \%lineHash;
$topHash{line2} = $lineHashRef;

Updated
Example given a loop over an array of data to parse

my %topHash;
foreach my $i (0 .. $#data) {
    my %tempHash;
    // stuff here to parse $data[$i] and populate %tempHash
    $topHash{"line$i"} = \%tempHash;
}
メ斷腸人バ 2024-08-16 03:50:25
#!/usr/bin/perl

use strict;

my %HoH = (
    line01 => {
        cell01 => "cell0101",
        cell02 => "cell0102",
        cell03 => "cell0103"
    }
);

$HoH{"line02"}    =
    {
        cell01 => "cell0201",
        cell02 => "cell0202",
        cell03 => "cell0203"
    };

foreach my $hohKey (keys %HoH)
{
    my $newHash = $HoH{$hohKey};
    print "Line Name: $hohKey\n";
    foreach my $key (keys %$newHash)
    {
        print "\t$key => ", $newHash->{$key}, "\n";
    }
}
#!/usr/bin/perl

use strict;

my %HoH = (
    line01 => {
        cell01 => "cell0101",
        cell02 => "cell0102",
        cell03 => "cell0103"
    }
);

$HoH{"line02"}    =
    {
        cell01 => "cell0201",
        cell02 => "cell0202",
        cell03 => "cell0203"
    };

foreach my $hohKey (keys %HoH)
{
    my $newHash = $HoH{$hohKey};
    print "Line Name: $hohKey\n";
    foreach my $key (keys %$newHash)
    {
        print "\t$key => ", $newHash->{$key}, "\n";
    }
}
十二 2024-08-16 03:50:25

每次从一行数据创建新的哈希时,您都需要考虑一个唯一的键来将该数据存储在顶级哈希表中。

my $line = 1;
my %HoH;
while (<>) {
    my ($cell01, $cell02, $cell03, @etc) = split /,/;
    my $newHash = { cell01 => $cell01, cell02 => $cell02, ... };
    my $key = "line$line";
    $HoH{$key} = $newHash;
    $line++;
}

现在 keys(%HoH) 将返回一个(未排序的)列表,例如 "line1","line2","line3",...
$HoH{"line5"} 将返回对文件第 5 行数据的引用。
%{$HoH{"line7"}} 是一种丑陋的语法,但它返回数据的哈希表
从第 7 行开始。
$HoH{"line14"}{"cell02"} 可用于获取特定的数据。

Everytime you create a new hash from a line of data, you'll need to think of a unique key to store that data in your top hash table.

my $line = 1;
my %HoH;
while (<>) {
    my ($cell01, $cell02, $cell03, @etc) = split /,/;
    my $newHash = { cell01 => $cell01, cell02 => $cell02, ... };
    my $key = "line$line";
    $HoH{$key} = $newHash;
    $line++;
}

Now keys(%HoH) will return a (unsorted) list like "line1","line2","line3",....
$HoH{"line5"} would return a reference to the data for the 5th line of your file.
%{$HoH{"line7"}} is kind of ugly syntax but it returns a hashtable of your data
from line 7.
$HoH{"line14"}{"cell02"} could be used to get at a specific piece of data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文