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

Hi PerlMonks,

I need to process the elements of a hash into a string. This so that the string can be used as a named parameter call in a method.

Given below is a sample hash

$VAR1 = [ { 'description' => '$description', 'new-role-name' => '$new-role-name', 'role-name' => '$role-name', } ];
This needs to be processed into : "description => $description, new-role-name => $new-role-name, role-name => $role-name" The code that i've written :
while(my ($param_key, $param_value) = each(%{$args->{parameter_set}})) + { $param_string.= "$param_key => $param_value,"; } print "\n$param_string\n";
gives the output
"description => $description, new-role-name => $new-role-name, role-na +me => $role-name,"

What's not correct in the output above is the comma at the end of the string.How do i correct this ?

Thanks in advance!

Replies are listed 'Best First'.
Re: Transform hash elements into a string
by wfsp (Abbot) on Aug 20, 2010 at 06:48 UTC
    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
      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
Re: Transform hash elements into a string
by Ratazong (Monsignor) on Aug 20, 2010 at 06:39 UTC

    There are many solutions to this. I immediately can think of

    • use a regex to replace the last comma by nothing
    • use substr to extract the first stringlength - 1 letters
    • do some logic not to write the last comma at all (e.g. write it before the key/value-pair - except for the first time)
    HTH, Rata
Re: Transform hash elements into a string
by Marshall (Canon) on Aug 20, 2010 at 08:51 UTC
    Update: Well I've been chastised for perhaps reading more into the question than there really is - to answer the OP's question: $param_string =~ s/,\s*$//; will delete the trailing ','. This part: named parameter call in a method. lead me astray into thinking that OP is passing parms into a method or sub say like the Tk library needs or whatever. Maybe that is not true. Ok, fair enough.

    Not exactly sure about what you are doing, but normally these hash => value pairs are passed as essentially an array of paired values - this is not a string. Don't confuse what the source code looks like with what the subroutine or method sees. To get $new-role-name to be translated into a value instead of this literal string, you just need "" instead of '' in the hash. Oh, this '=>' thingie is called a "fat comma" and as you can see below that is pretty much what happens, ie. works like a comma.

    #!/usr/bin/perl -w use strict; my %hash = ( 'description' => '$description', 'new-role-name' => '$new-role-name', 'role-name' => '$role-name', ); x(%hash); #this makes param,value pairs from %hash print "\n"; my $abc = 99999; x("value" => 12, "xyz" => "$abc"); sub x { my @input = @_; print "array representation: @input\n"; my %hash = @_; print "hash is:\n"; foreach (keys %hash) { print "$_ => $hash{$_}\n"; } } __END__ prints: array representation: role-name $role-name new-role-name $new-role-nam +e description $description hash is: new-role-name => $new-role-name role-name => $role-name description => $description array representation: value 12 xyz 99999 hash is: value => 12 xyz => 99999
Re: Transform hash elements into a string
by LanX (Saint) on Aug 20, 2010 at 09:09 UTC
    IIRC you can configure Data::Dumper for many kind of such formats.

    Cheers Rolf

Re: Transform hash elements into a string
by csebe (Initiate) on Dec 18, 2014 at 11:35 UTC

    In this case join is clearly more indicated however, sometimes I construct csv strings by appending to it (conditionally) values like "xxx," then "yyy," repeatedly. (As you cannot know which one will be the last, you cannot afford not to have the commas after each one.)

    In this case, a simple chop after will do the trick:

    [...] chop($param_string); print "\n$param_string\n";

    Cheers