Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

by jaa (Friar)
on Jul 29, 2005 at 09:26 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://479315]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-19 19:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found