tags:

views:

112

answers:

2

The way a hash is structed can always vary, it can be a hash of a hash of an array or whatever. And for every different struct of a hash there needs to be a different implementation of turning it into a two dimensional array.

Is there a general way of converting a hash into an array? Such that i could say, for instance, first key becomes column 0, second key column 1 etc.

Example from comments:

$distangle{some_distance}{some_angle}=(); now I want to convert that hash of hashes into an ordinary two dimensional array @distangle=(some_distance,some_angle). That's a method, then tomorrow I have some different form of a hash I also need to convert to a two dimensional array.

A: 

What is "first" key in a hash? The keys are not ordered. Do you want to order them alphabetically?

@arr = map { $hash{$key} } sort keys %hash;

EDIT:

OP wants a 2D array, so here it is:

@arr = ()
for $first (keys %hash) {
    for $second (keys %{ $hash{$first} }) {
        for $third (keys %{ $hash{$first}{$second} }) {
            my $value = $hash{$first}{$second}{$third};
            push @arr, ($first, $second, $third, $value);

Something like this?

EDIT 2: This solution looks nice too.

eumiro
$cars{type}{color}{number}=155; in this one, 'type' is the first key.
Hermann Ingjaldsson
Should use curly bracer to get value by key `$hash{$key}` in hash.
Ivan Nevostruev
@Ivan - thanks, that comes from Python ;-)
eumiro
@Hermann - do you want to create a 3-dimensional array?
eumiro
no two dimensional, that was just to demonstrate what i mean by first key.
Hermann Ingjaldsson
@Hermann What array do you want to get from `$cars{type}{color}{number}` ? And what you're trying to achieve?
Ivan Nevostruev
i have all kinds of different hashes, and im routinely converting them all to two dimensional arrays, and consequently im doing the same thing multiple times.
Hermann Ingjaldsson
@Hermann Can you please give an example?
Ivan Nevostruev
$distangle{some_distance}{some_angle}=(); now i want to convert that hash of hashes into an ordinary two dimensional array @distangle=(some_distance,some_angle). that's a method, then tomorrow i have some different form of a hash i also need to convert to a two dimensional array.
Hermann Ingjaldsson
regarding edit of current answer, no im talking about a single method with which i can convert a hash of any sort into a two dimensional array, that method given would work with a hash of hash of hash of elements, but not hash of arrays or hash of hashes or.. all the other ones.
Hermann Ingjaldsson
@eumiro `@arr = []` will create array with `[]` as element in it. I guess you need `@arr = ()` instead.
Ivan Nevostruev
@Ivan - thank you
eumiro
@Hermann - the second edit gives you a link to a nice solution. Wasn't it shown to you as you posted your original question?
eumiro
it may well have,i find the user interface very lacking in readability and simplicity though, but it's in the direction. the solution is off course, a recursive one.
Hermann Ingjaldsson
A: 

Firstly, hashes are unordered, so when you say the "first key", there's no such thing.

Secondly, if you do have a hash of hash of arrays (as in your example), then it seems to me that the strict requirement of reducing it to a 2-dimensional array will result in data-loss (assuming you mean that none of the elements in that array can be hashrefs or arrayrefs).

ishnid
$cars{type}{color}{number}=155; in this one, 'type' is the first key. Yes data loosing is an issue when converting like this.
Hermann Ingjaldsson
Ah ok. The example you've posted helps explain things.
ishnid