簡體   English   中英

perl 中的引用:數組到另一個數組的散列

[英]references in perl: hash of array to another array

我在將數組中的散列引用到另一個數組時遇到問題。 我有一個數組@result,它看起來像這樣:

@result = (

{ "type" => "variable",
  "s" => "NGDP",
  "variable" => "NGDP" },

{"type" => "subject",
  "s" => "USA",
  "subject" => "USA",
  "variable" => "NGDP" },

{ "type" => "colon",
  "s" => ",",
  "colon" => "," }, 

{ "type" => "subject",
  "s" => "JPN",
  "subject" => "JPN",
  "variable" => "NGDP" },

{ "type" => "operator",
  "s" => "+",
  "operator => "+" },

{"type" => "subject",
  "s" => "CHN",
  "subject" => "CHN",
  "variable" => "NGDP" },
);

我想將此數組划分為冒號並將@result 數組的元素推送到另一個數組,因此我編寫了腳本:

for ($i = 0; $i <= $#result; $i++) {  

if (defined $result[$i]{subject} or $result[$i]{operator} and not defined $result[$i]{colon}) {
  push @part_col, \%{$result[$i]};
}

elsif ($i == $#result) {
  push @part_col_all, \@part_col;
}

elsif (defined $result[$i]{colon}) {
  push @part_col_all, \@part_col;
  my @part_col;
 }
}

所以我需要的是,如果我打印出$part_col_all[0][0]{subject}結果將是"USA"

對於$part_col_all[1][0]{subject}將是"JPN"

對於$part_col_all[1][1]{operator}將是"+"等。

我對$part_col_all[0][0]{subject}"USA"

對於$part_col_all[0][1]{subject}"JPN" ,它應該在$part_col_all[1][0]{subject}

$part_col_all[0][3]{subject}"CHN" ,而它應該在 $part_col_all[1][2]{subject} 中。

我正在制作一個應用程序,它根據某個經濟輸入從經濟數據創建圖表。 @result 數組是我的預處理輸入,我知道哪個變量屬於哪個國家。 如果我得到像 GDP USA CAN、JPN+CHN 這樣的輸入,我需要將此輸入拆分為 GDP USA CAN 和 JPN+CHN。 這就是為什么我做了一個條件,如果找到冒號,把@part_col 中的所有內容推送到@part_col_all 的第一個元素,然后如果它在輸入的末尾,則將JPN+CHN 推送到@push_col_all 的第二個元素。

所以@part_col_all 應該是這樣的:

@part_col_all = (
    (   
        {"type" => "subject",
        "s" => "USA",
        "subject" => "USA",
        "variable" => "NGDP" },

        {"type" => "subject",
        "s" => "CAN",
        "subject" => "CAN",
        "variable" => "NGDP" },
    ),

    (
        { "type" => "subject",
        "s" => "JPN",
        "subject" => "JPN",
        "variable" => "NGDP" },

        { "type" => "operator",
        "s" => "+",
        "operator" => "+" },

        {"type" => "subject",
        "s" => "CHN",
        "subject" => "CHN",
        "variable" => "NGDP" },

    )
);

我不知道我做錯了什么。 抱歉,如果有任何基本錯誤,我是初學者。 非常感謝。

首先,你缺少一個報價:

{ "type" => "operator",
  "s" => "+",
  "operator" => "+" },
           ^ missing

至於打印,您可以執行以下操作:

foreach my $part (@part_col){
    print $part->{operator}."\n";
}

或者在打印周期中使用這些值做任何你想做的事情

您應該閱讀Perl 參考教程來幫助您。

取消引用以簡化您的代碼並沒有錯:

my @part_col;
my @part_col_all;

for $i ( 0..$#array ) {
    my %hash = ${ $result[$i] };  # Make it easy on yourself. Dereference
    if ( defined $hash{subject} or defined $hash{operator} and not defined $hash{colon} ) {
        push @part_col, \%hash;  # or push, @par_col, $result[$i]
    }
}

請注意,我將for從您必須的三部分設置更改for更清晰、更易於理解的表述方式。

仔細觀察您的數據結構,我注意到$hash{type}會告訴您是否定義了$hash{operator}$hash{subject}$hash{colon} 讓我們只使用$hash{type}並簡化它, if

my @part_col;
my @part_col_all;

for my $i ( 0..$#array ) { 
    my %hash = ${ $result[$i] };  # Make it easy on yourself. Dereference
    if ( $hash{type} eq "subject" or $hash{type} eq "operator" ) {
        push @part_col, \%hash;  # or push, @par_col, $result[$i]
    }
}

事實上,由於@array只是一個數組,我會把它當作一個數組。 我將使用一個簡單的for結構來遍歷數組的每個元素。 每個元素都是一個 hash_reference,所以:

for my $hash_ref ( @array ) {
    my %hash = %{ %hash_ref };
    if ( $hash{type} eq "subject" or $hash{type} eq "operator" ) {
        push @part_col, \%hash;
    }
}

進一步簡化,我可以使用->語法一次性取消引用並討論我的哈希的特定元素:

for my $hash_ref ( @array ) {
    if ( $hash_ref->{type} eq "subject" or $hash_ref->{type} eq "operator" ) {
        push @part_col, $hash_ref;
    }
}

我正在嘗試了解您的其余代碼:

    elsif ($i == $#result) {
        push @part_col_all, \@part_col;
    }

    elsif (defined $hash_ref->{colon}) {
        push @part_col_all, \@part_col;
        my @part_col;
    }
}

這些@part_col@part_col_all讓我困惑。 您到底想在@part_col_all存儲@part_col_all 請記住, \\@part_col 是您在內存中存儲@part_col 您一遍又一遍地將相同的內存位置推送到該哈希上,因此您一遍又一遍地存儲相同的引用。 這真的是你想要的嗎? 我對此表示懷疑。

您需要做的是確定您的數據結構究竟代表什么。 數據結構應該有一個可靠的定義。 @part_col_all的數據結構代表什么? $part_col_all[$i]的數據結構代表什么? $part_col_all[$i]->[$j]的數據結構代表什么? 如果不知道這一點,就很難回答您的其余問題。

您是否將類型為colon元素存儲在一個數組中,並將其他所有元素存儲在另一個數組中? 或者您是否將所有內容都存儲在一個數組中,而在另一個數組中存儲所有不是colon類型的內容?

一旦我明白了這一點,我就可以回答你的其余問題。

附錄

感謝您的回復,我會嘗試這種方式並寫下我的結果。 這真的很有幫助。 我用有關@part_col_all 的數據結構的更多信息更新了我的問題。 我希望你能理解我要解釋的內容,如果沒有,我會再試一次。


如果我明白你在做什么,有人會輸入NGDP USA , JPN+CNA這意味着你正在比較美國與日本和中國的 NGDP 總和。

在我看來,您需要三個單獨的變量:

  • $parameter - 你在測量什么。 (GDP等)
  • @countries_set_1 - 第一組國家
  • @countries_set_2 - 您與第一組比較的第二組國家。

而且,你所說的冒號(我們在美國稱之為逗號)作為第一組國家與第二組國家之間的分隔符。 然后,您只需通過一個循環即可。 可能兩個數組只是同一個數組的兩個元素,國家集合是數組引用。 我想象這樣的事情:

@input = qw(GDP USA, JPN CHN); # Compare the GDP of the USA with Japan and China together
my $parameter = shift @input;  # Remove what you're measuring
my @country_sets;              # An array of arrays
my $set = 0                    # Which set you're on
for my $value ( @input ) {
    if ( $value eq "," ) {
       $set += 1;              # Next Set
       next;
    }
    push @{ $country_sets[$set] }, $input;
}

這將創建一個這樣的數據結構:

@country_sets  = (
                     (
                          USA,
                     ),
                     (
                          JPN,
                          CHN,
                     ),
                 )

不需要復雜的@results因為您只需要為所有相關人員執行一個操作(GDP 等)。

不過,我想我明白你想要什么。 我們將使用一組數組。 這是我之前所擁有的:

for my $hash_ref ( @array ) {
    if ( $hash_ref->{type} eq "subject" or $hash_ref->{type} eq "operator" ) {
        push @part_col, $hash_ref;
    }
}

我們將把它和我上面提供的將國家分成兩組的代碼結合起來:

my @country_sets;              # An array of arrays
my $set = 0                    # Which set you're on
for my $country_ref ( @array ) {
    next if $country_ref->{type} eq "variable";  # We don't want variables
    if ( $country_ref{type} eq "colon" ) {       # Switch to the other country set
        set += 1;
        next;
    }
    push @{ $country_sets[$set] }, $country_ref;
}

前幾個條目將進入$country_sets[0] ,這將是一個數組引用。 冒號(不會被輸入到集合中)之后,第二組國家將進入$country_sets[1] ,這將是另一個 array_ref 到哈希引用:

  • @country_sets - 將輸入信息包含為兩組
  • @country_sets[$x] - 一組特定的國家(可能還有運營商)
  • @country_sets[$x]->[$y] - 一個特定的國家或運營商
  • @country_sets[$x]->[$y]->{$key} - 來自特定國家的特定值

$x01 這會給你這樣的東西:

$country_sets[0] = (
                       {
                          "type" => "subject",
                          "s" => "USA",
                           "subject" => "USA",
                           "variable" => "NGDP",
                       },
                   )

$country_sets[1] = (
                      {
                         "type" => "subject",
                         "s" => "JPN",
                         "subject" => "JPN",
                         "variable" => "NGDP",
                      },

                      { 
                          "type" => "operator",
                          "s" => "+",
                          "operator => "+",
                      },
                      {
                          "type" => "subject",
                          "s" => "CHN",
                          "subject" => "CHN",
                          "variable" => "NGDP",
                      },
                 );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM