Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: How do I turn a hash into a name=value string?

by jaa (Friar)
on Jul 29, 2005 at 09:26 UTC ( #479315=note: print w/replies, xml ) Need Help??


in reply to How do I turn a hash into a name=value string?

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2020-05-28 08:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (165 votes). Check out past polls.

    Notices?