如何从散列哈希中提取所有数组值

时间:2023-01-01 12:42:29

Newbie here. Aplogies if I am missing details.

新手在这里。如果我遗漏细节,请记录下来。

In perl 5

在perl 5

I have a file that kind of looks like this

我有一个看起来像这样的文件

precedence = 2
new york
new jersey
florida
precedence = 3
kings
essex
dade
precedence = 1
*lyn
newark
miami

I have no problem looping through the file and creating a $var that holds the value of precedence and an array (@tmp) that holds the lines until the next "section" (precedence = x)

我没有问题循环文件并创建一个保存优先级值的$ var和一个数组(@tmp),它保存行直到下一个“section”(优先级= x)

I need to ultimately push all the sections into a final array in the order of the preference

我需要最终按照首选项的顺序将所有部分推送到最终数组中

so

print @final;

results in

 *lyn
 .....
 new york
 .....
 kings
 .....

NOTE: I never know in advance how many sections there will be or how many lines each section will have

注意:我从来不知道将会有多少部分或每个部分将有多少行

I thought perhapes to make a Hash of hashes and put each array in the HoH

我认为perhapes可以制作散列哈希并将每个数组放入HoH中

push @{ $hash{"section_2"} }, @tmp ;

but I didnt know

但我不知道

a) if there would be a problem reusing the @tmp array each time i load a section in (after blanking it at the beginning of each loop)

a)如果每次加载一个部分(在每个循环开始时消隐之后)重用@tmp数组会出现问题

b) I couldnt figure out how to get all values in the array in key "section_2" and push them into @final

b)我无法弄清楚如何在关键字“section_2”中获取数组中的所有值并将它们推入@final

Of course there may be a better approach.

当然可能有更好的方法。

2 个解决方案

#1


2  

An HoH makes no sense. You could use an HoA if you expect a wide variance in precedence levels (1, 1000000, 1000000000),

HoH毫无意义。如果您预计优先级差异很大(1,1000000,1000000000),则可以使用HoA,

my $precedence = 0;
my %data;
while (<>) {
   chomp;
   if (/precedence\s*=\s*([0-9]+)\z/) {
      $precedence = $1;
      next;
   }

   push @{ $data{$precedence} }, $_;
}

my @final = map @{ $data{$_} }, sort { $a <=> $b } keys %data;

but an AoA would most likely be a better fit.

但AoA最有可能更合适。

my $precedence = 0;
my @data;
while (<>) {
   chomp;
   if (/precedence\s*=\s*([0-9]+)\z/) {
      $precedence = $1;
      next;
   }

   push @{ $data[$precedence] }, $_;
}

my @final = map @$_, grep $_, @data;

#2


0  

Not really sure that I fully understand what you are trying to accomplish but if you want to print each precedence value and then the values from its array, you can try this:

不确定我是否完全理解您要完成的任务,但如果您想要打印每个优先级值,然后打印其数组中的值,您可以尝试这样做:

my $hoh;

#This is not how you populate your HoH, I hard code it to simplify
@{$hoh->{2}->{'ARRAY'}} = ('new york', 'new jersey', 'florida');
@{$hoh->{3}->{'ARRAY'}} = ('kings', 'essex', 'dade');
@{$hoh->{1}->{'ARRAY'}} = ('*lyn', 'newark', 'miami');

foreach my $prcdnc(keys(%$hoh))
{
    print "\nprcdnc = ".$prcdnc;

    my @prcdncAry = @{$hoh->{$prcdnc}->{'ARRAY'}};

    my $prcdncAryStr = join(",", @prcdncAry);

    print "\n\t".$prcdncAryStr;
}

#1


2  

An HoH makes no sense. You could use an HoA if you expect a wide variance in precedence levels (1, 1000000, 1000000000),

HoH毫无意义。如果您预计优先级差异很大(1,1000000,1000000000),则可以使用HoA,

my $precedence = 0;
my %data;
while (<>) {
   chomp;
   if (/precedence\s*=\s*([0-9]+)\z/) {
      $precedence = $1;
      next;
   }

   push @{ $data{$precedence} }, $_;
}

my @final = map @{ $data{$_} }, sort { $a <=> $b } keys %data;

but an AoA would most likely be a better fit.

但AoA最有可能更合适。

my $precedence = 0;
my @data;
while (<>) {
   chomp;
   if (/precedence\s*=\s*([0-9]+)\z/) {
      $precedence = $1;
      next;
   }

   push @{ $data[$precedence] }, $_;
}

my @final = map @$_, grep $_, @data;

#2


0  

Not really sure that I fully understand what you are trying to accomplish but if you want to print each precedence value and then the values from its array, you can try this:

不确定我是否完全理解您要完成的任务,但如果您想要打印每个优先级值,然后打印其数组中的值,您可以尝试这样做:

my $hoh;

#This is not how you populate your HoH, I hard code it to simplify
@{$hoh->{2}->{'ARRAY'}} = ('new york', 'new jersey', 'florida');
@{$hoh->{3}->{'ARRAY'}} = ('kings', 'essex', 'dade');
@{$hoh->{1}->{'ARRAY'}} = ('*lyn', 'newark', 'miami');

foreach my $prcdnc(keys(%$hoh))
{
    print "\nprcdnc = ".$prcdnc;

    my @prcdncAry = @{$hoh->{$prcdnc}->{'ARRAY'}};

    my $prcdncAryStr = join(",", @prcdncAry);

    print "\n\t".$prcdncAryStr;
}