Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Converting HoA into AoH

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


in reply to Converting HoA into AoH

This is my previous function rewritten to handle spaces in the data by escaping them. Other surprises may be handled similarly.

#!/usr/bin/perl use Data::Dumper; sub hoa2aoh { my $hoa = shift; my @keys = keys %$hoa; my @patterns = map { local $" = ','; "{@{[map {s/ /\\ /g; $_} @$_]}}"; } values %$hoa; [ map { my %hash; @hash{@keys} = split ','; \%hash; } glob join ',', @patterns ]; } my $HoA = { '1,2,flintstones' => [ 'fred-1 foo-2', 'barney-1 bar-2' ], '2,3,jetsons' => [ 'george-1 foo-2', 'jane-1 bar-2'], }; print Dumper hoa2aoh($HoA); __END__ $VAR1 = [ { '1,2,flintstones' => 'fred-1 foo-2', '2,3,jetsons' => 'george-1 foo-2' }, { '1,2,flintstones' => 'barney-1 bar-2', '2,3,jetsons' => 'george-1 foo-2' }, { '1,2,flintstones' => 'fred-1 foo-2', '2,3,jetsons' => 'jane-1 bar-2' }, { '1,2,flintstones' => 'barney-1 bar-2', '2,3,jetsons' => 'jane-1 bar-2' } ];

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: Converting HoA into AoH
by robin (Chaplain) on Oct 31, 2005 at 17:40 UTC

    Using glob is a delightful trick, but in general you really need to escape the string much more than this. (To see what I mean, try putting a * in one of the strings.)

    Instead of s/ /\\ /g, use s/(\W)/\\\\\\$1/g. I know itís a lot of backslashes, but you really do need that many here!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2020-05-31 03:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (173 votes). Check out past polls.

    Notices?