in reply to Transform hash elements into a string

join is handy for this sort of thing.
#! /usr/bin/perl use strict; use warnings; my %hash = ( 'description' => '$description', 'new-role-name' => '$new-role-name', 'role-name' => '$role-name', ); my $param = join(q{, }, map{qq{$_ => $hash{$_}}} keys %hash); print $param;
role-name => $role-name, new-role-name => $new-role-name, description +=> $description

Replies are listed 'Best First'.
Re^2: Transform hash elements into a string
by FloydATC (Deacon) on Aug 20, 2010 at 11:26 UTC
    I usually do it with join() too but write it out in plain code simply because I find map{} more confusing than it's worth:

    my @pairs = (); while ( my ($key,$value) = each %hash) { push @pairs, $key . ' => ' . $value; } print join(', ', @pairs);

    -- Time flies when you don't know what you're doing