Remove mutiple values from a hash in perl -


i need iterate through hash in perl , if key multiple values present in form of array , take first value. code snippet

while (my ($key, $value) = each(%args)) {     print "$key-----------------------------$value\n\n";     if ($key =~ /submit\.([\w-]+)/) {         $submitfield = $1;     } elsif  ($key =~ /action/) {         $submitfield = $value;     }     if (ref($value) ) {         if (%min_args)         {             print ("min args not found aborting request");             $m->comp('/x-locale/errors/404.m');             $m->abort();         }         print "multiple value found $key";         $value = shift @$value if ref($value) eq "array";         $args{$key} = $value;         print "value changed $args{$key}\n\n";         print data::dumper->dump([$value]);         print "<pre>" . data::dumper->dump([%args]) . "</pre>";     } } 

the problem if pass in more 1 key having array of values , doing shift operation first 1 , coming out of loop. ideas happening here?

you have not demonstrated problem, , missing bits added, expected result.

use strict; use warnings;  use data::dumper;  %args = (    => 'b',    c => [ 'd', 'e' ],    f => [ 'g', 'h' ],    => 'j', );  while (my ($key, $value) = each(%args)) { # print "$key-----------------------------$value\n\n"; # if ($key =~ /submit\.([\w-]+)/) { #    $submitfield = $1; # } elsif  ($key =~ /action/) { #    $submitfield = $value; # }  if (ref($value) ) { #    if (%min_args) { #       print ("min args not found aborting request"); #       $m->comp('/x-locale/errors/404.m'); #       $m->abort(); #    } #   print "multiple value found $key";    $value = shift @$value if ref($value) eq "array";    $args{$key} = $value; #   print "value changed $args{$key}\n\n"; #   print data::dumper->dump([$value]); #   print "<pre>" . data::dumper->dump([%args]) . "</pre>"; data::dumper->dump([%args]);  } }  print dumper(\%args); 

output:

$var1 = {           'c' => 'd',           'a' => 'b',           'f' => 'g',           'i' => 'j'         }; 

mind you, there's funky stuff going on. each hash has 1 iterator, data::dumper call inside loop sees part of hash. resets iterator, causing each restart. avoid visiting same keys repeatedly, move call data::dumper outside of loop (where should be) or use for keys instead of while each gets keys upfront.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -