1

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.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
john-jones
  • 7,490
  • 18
  • 53
  • 86
  • 1
    How do *keys* "become" *columns*? Your problem is not well defined. Are you talking about a *grid*? – Axeman Sep 23 '10 at 16:03
  • 2
    What problem are you trying to solve? There's probably a better way to do it. Always start with what you are trying to accomplish rather than how you think you should do it. See the Perlmonks writeup on [XY Problems](http://www.perlmonks.org/index.pl?node_id=542341). – brian d foy Sep 23 '10 at 16:41
  • @axeman:hash is one way of storing information, array is another. so when i convert the hash then its keys are a part of the information, so i put it in the array. im talking about two dimensional arrays. – john-jones Sep 23 '10 at 16:50
  • @brian im trying to be able to convert miscellaneous types of hashes into two dimensional arrays using a single solution. – john-jones Sep 23 '10 at 16:51
  • 1
    No, that's your solution. I want to know what your problem is. What is your starting state and your desired end state? Forget about what you think the solution is and just explain the task. – brian d foy Sep 23 '10 at 17:06

2 Answers2

0

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.

Community
  • 1
  • 1
eumiro
  • 207,213
  • 34
  • 299
  • 261
  • $cars{type}{color}{number}=155; in this one, 'type' is the first key. – john-jones Sep 23 '10 at 13:56
  • Should use curly bracer to get value by key `$hash{$key}` in hash. – Ivan Nevostruev Sep 23 '10 at 13:56
  • no two dimensional, that was just to demonstrate what i mean by first key. – john-jones Sep 23 '10 at 13:59
  • @Hermann What array do you want to get from `$cars{type}{color}{number}` ? And what you're trying to achieve? – Ivan Nevostruev Sep 23 '10 at 13:59
  • 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. – john-jones Sep 23 '10 at 14:00
  • $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. – john-jones Sep 23 '10 at 14:08
  • 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. – john-jones Sep 23 '10 at 14:15
  • @eumiro `@arr = []` will create array with `[]` as element in it. I guess you need `@arr = ()` instead. – Ivan Nevostruev Sep 23 '10 at 14:16
  • @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 Sep 23 '10 at 14:19
  • 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. – john-jones Sep 23 '10 at 14:32
0

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
  • 539
  • 2
  • 5