Traversing a multi-dimensional hash in Perl

后端 未结 8 1955
后悔当初
后悔当初 2021-01-03 04:02

If you have a hash (or reference to a hash) in perl with many dimensions and you want to iterate across all values, what\'s the best way to do it. In other words, if we hav

8条回答
  •  难免孤独
    2021-01-03 04:18

    You can also fudge multi-dimensional arrays if you always have all of the key values, or you just don't need to access the individual levels as separate arrays:

    $arr{"foo",1} = "one";
    $arr{"bar",2} = "two";
    
    while(($key, $value) = each(%arr))
    {
        @keyValues = split($;, $key);
        print "key = [", join(",", @keyValues), "] : value = [", $value, "]\n";
    }
    

    This uses the subscript separator "$;" as the separator for multiple values in the key.

提交回复
热议问题