references in perl: hash of array to another array -


i have problem referencing hash in array array. have array @result looks this:

@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" }, ); 

i want divide array colons , push elements of @result array array, wrote script:

for ($i = 0; $i <= $#result; $i++) {    if (defined $result[$i]{subject} or $result[$i]{operator} , 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;   @part_col;  } } 

so need if print out $part_col_all[0][0]{subject} result "usa",

and $part_col_all[1][0]{subject} "jpn",

and $part_col_all[1][1]{operator} "+" etc.

my result $part_col_all[0][0]{subject} "usa"

and $part_col_all[0][1]{subject} "jpn" should in $part_col_all[1][0]{subject}.

the result $part_col_all[0][3]{subject} "chn", while should in $part_col_all[1][2]{subject}.

i'm making application creating graphs economical data based on economical input. @result array preprocessed input know country variable belongs. if input gdp usa can, jpn+chn need split input gdp usa can , jpn+chn. that's why made condition, if colon found, push in @part_col first element of @part_col_all, , if it's on end of input, push jpn+chn second element of @push_col_all.

so @part_col_all should looks this:

@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" },      ) ); 

i dont know i'm doing wrong. sorry if there basic mistakes, im beginner. lot.

first, you're missing quote:

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

as printing, can following:

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

or whatever want in print cycle values


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -