http://www.perlmonks.org?node_id=479035

jaa has asked for the wisdom of the Perl Monks concerning the following question:

I have "name=value,..." strings that I convert to hashes like this:
my ( %hash ) = split /\s*[=,]\s*/, "apples=green,pears=brown,oranges +=orange";
Do the Monks have any elegant solutions for the inverse? I.e. converting %hash back into the original string?

Thanks in advance!

Regards,

Jeff

Replies are listed 'Best First'.
Re: How do I turn a hash into a name=value string?
by JediWizard (Deacon) on Jul 28, 2005 at 16:48 UTC

    my $string = join(",", map({"$_=$hash{$_}"} keys %hash));

    Will that work?


    They say that time changes things, but you actually have to change them yourself.

    —Andy Warhol

Re: How do I turn a hash into a name=value string?
by Fang (Pilgrim) on Jul 28, 2005 at 17:01 UTC

    All the above solutions work, just remember that when you join your data back, you won't necessarily obtain a string identical to the original one. If you want your hash to remember the order in which you put elements into it, read perldoc -q order.

Re: How do I turn a hash into a name=value string?
by sh1tn (Priest) on Jul 28, 2005 at 17:23 UTC
    $_ .= "$k=$v," while ($k,$v) = each %hash;


      Mmmm, nice alternative, but not with USE STRICT?

      Regards,

      Jeff

Re: How do I turn a hash into a name=value string?
by bangers (Pilgrim) on Jul 28, 2005 at 16:49 UTC
    Not sure I'd call this elegant, but:
    my ( %hash ) = split /\s*[=,]\s*/, "apples=green,pears=brown,oranges=o +range"; my ($string) = join(",", (map { my $a = "$_=$hash{$_}" } keys(%hash)) + ); print "[$string]\n";

      Out of curiosity: Why are you bothering to assign to a variable inside your map statement?

      I have not tested it, but I am pretty sure if you run with warnings and strict you will be warned the $a is used only once. Beyond that you really should not use $a or $b as they are special variables used for sort.


      They say that time changes things, but you actually have to change them yourself.

      —Andy Warhol

        I tried something elase and first, then decided the above way was better. But you're right, it isn't needed - although I always run with strict and there aren't any warnings.
Re: How do I turn a hash into a name=value string?
by jaa (Friar) on Jul 29, 2005 at 09:26 UTC
    Thanks for the suggestions! I have now been introduced to EACH - something I have never really used.

    It also made me read the JOIN doc more carefully - here is an inefficient, but fun OOlternative :).

    use strict; my %hash = ( apple => 'pie', banana => 'custard', cherry => 'ripe', ); # use map to alternate $delim between = and , my $delim = ''; my $str = join $delim, map { $delim = ($delim eq ',' ? '=' : ','); $_ +} %hash; print "result: [$str]\n"; # use class stringification overload to alternate my $str2 = join( flipper->new(), %hash ); print "result: [$str2]\n"; #---------------------------------- package flipper; use overload '""' => \&toString; sub new { my $class = shift; my $self = { counter => 0 }; bless $self, $class; } sub toString { my $self = shift; return ( $self->{counter}++ % 2 ? '=' : ',' ); }

    Does anyone have a more elegant use of the fact that JOIN stringifies the EXPR for each element? perhaps an anon-sub closure??

    Regards,

    Jeff