Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: Converting HoA into HoH

by Zaxo (Archbishop)
on Oct 31, 2005 at 06:28 UTC ( #504172=note: print w/replies, xml ) Need Help??

in reply to Converting HoA into AoH

Your desired output looks more like an AoH. It doesn't have any keys in the top level.

Yet another combinatorial problem. Let's try glob again,

sub hoa2aoh { my $hoa = shift; my @keys = keys %$hoa; my @patterns = map { local $" = ','; "{@$_}"; } values %$hoa; [ map { my %hash; @hash{@keys} = split ','; # char changed from "\0" \%hash; } glob join ',', @patterns ]; }
As I say, that returns a reference to an array of hashes.

Update: glob wasn't liking the "\0" seperator I used at first. It didn't like whitespace, either. Changed to comma, it works now. Also removed superfluous variable.

After Compline,

Replies are listed 'Best First'.
Re^2: Converting HoA into HoH
by neversaint (Deacon) on Oct 31, 2005 at 06:35 UTC
    Dear Zaxo,
    Thanks also for your response. Two points here. Why your subroutine above takes 2 arguments as input?

    Another point is, I tried your code,
    my $ref = hoa2aoh($HoA); print Dumper $ref;
    it returns:
    $VAR1 = [ { 'flintstones' => undef, 'jetsons' => 'george' }, { 'flintstones' => undef, 'jetsons' => 'jane' } ];
    Please correct me if I misinterpret how your function works.

    neversaint and everlastingly indebted.......

      I had an error in the argument to glob, repaired as noted above.

      The superfluous variable was $hoh in the line

      my ($hoa, $hoh) = shift;
      That doesn't really involve two arguments - the shift only brings in one value, and $hoh remained undefined and unused. I only thought I'd want it at first, and piggybacked its declaration on the arg line.

      After Compline,

        Dear Zaxo,
        One last thing here. I'm really sorry for not being precise earlier. I was just trying to pose the problem as simple as possible. The element of the array in my actual production code comes with white space like this:
        my $HoA = { '1,2,flintstones' => [ "fred-1 foo-2", "barney-1 bar-2" ], '2,3,jetsons' => [ "george-1 foo-2", "jane-1 bar-2"], };
        The key of the new hash is identified correctly, the problem is only in the values. Is there a way I can modify your code? In particular, so that it takes the element of the array as "one" entity regardless the white-space?
        So that it doesn't return:
        $VAR1 = [ { '1,2,flintstones' => undef, '2,3,jetsons' => undef }, { '1,2,flintstones' => 'jane-1', '2,3,jetsons' => 'foo-2' }, { '1,2,flintstones' => undef, '2,3,jetsons' => 'bar}' }, { '1,2,flintstones' => 'barney-1', '2,3,jetsons' => 'foo-2' }, { '1,2,flintstones' => undef, '2,3,jetsons' => 'bar-2}' } ];
        But simply:
        my $AoH = [ { '1,2,flinstones' => "fred-1 foo-2", '2,3,jetsons' => "george-1 foo-2" }, { '1,2,flinstones' => "fred-1 foo-2", '2,3,jetsons' => "jane-1 bar-2" }, { '1,2,flinstones' => "barney-1 bar-2", '2,3,jetsons' => "george-1 foo-2" }, { '1,2,flinstones' => "barney-1 bar-2", '2,3,jetsons' => "jane-1 bar-2" }, ];
        I tried to modify your code by changing the type of separator. But doesn't work either. Hope to hear from you again.

        neversaint and everlastingly indebted.......

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://504172]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2020-06-02 12:19 GMT
Find Nodes?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?

    Results (17 votes). Check out past polls.