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

Hi monks,

I have a number of database queries that return array references. I'm then putting each array reference into a hash so I can return it as json.

I want to modify each arrayref to produce a comma separated string. I've tried using the following to convert each reference and add it to my hash:

$return->{$w} = [join(', ', map { $_->[0] } @$rows)];
This returns "key":["val1, val2"] but I need each word to be quoted like this "key":["val1", "val2"].

Can anyone spot what I'm doing wrong?

Replies are listed 'Best First'.
Re: Quoting each word in an arrayref
by Corion (Patriarch) on Jul 22, 2016 at 08:47 UTC

    Why do you use join(', ', ... when you don't want a string?

    Most JSON modules know how to deal with an arrayref, so construct the arrayref directly:

    $return->{$w} = [map { $_->[0] } @$rows];
      Thank you, that worked perfectly.

      @hippo. Thank you also. I am using a JSON module to encode the response but doing so gives me the data in this format [["val1"],["val2"]] which is why I was using map to get ["val1", "val2"]. Is there a better way to do this?

        Hi soundX,

        Is there a better way to do this?

        That depends, personally I like the map solution, but if your data structure is more complicated than the small example you've shown then perhaps there is an even better solution. Note that the solution given by Corion, based on your code, only pulls the first value out of the nested array, if the nested arrays have a different number of values in them then another approach might be more appropriate. Here are some variations to play around with (JSON output manually sorted):

        use warnings; use strict; use JSON::MaybeXS; my $rows = [["val1"],["val2"]]; my $rows_a = [[],["val2","val3"]]; my $return; $return->{'key1'} = [join(', ', map { $_->[0] } @$rows)]; $return->{'key2'} = [map { $_->[0] } @$rows]; $return->{'key3'} = [map { @$_ } @$rows]; $return->{'key2_a'} = [map { $_->[0] } @$rows_a]; $return->{'key3_a'} = [map { @$_ } @$rows_a]; print encode_json($return), "\n"; __END__ { "key1":["val1, val2"], "key2":["val1","val2"], "key3":["val1","val2"], "key2_a":[null,"val2"], "key3_a":["val2","val3"] }

        Hope this helps,
        -- Hauke D

Re: Quoting each word in an arrayref
by hippo (Archbishop) on Jul 22, 2016 at 09:06 UTC
    Can anyone spot what I'm doing wrong?

    What you are doing wrong is not using JSON::XS (or JSON::PP) because these will handle quoting and all the other JSON syntax for you. Make life easy on yourself and use a module. Never forget that laziness is one of the three great virtues.

Re: Quoting each word in an arrayref
by RonW (Parson) on Jul 25, 2016 at 19:50 UTC
    This returns "key":"val1, val2" but I need each word to be quoted like this "key":"val1", "val2".

    Aside from the fact that your intention is to produce JSON output, so using a JSON module would be the best solution, your question also brings up a more general problem: quoting items in a list.

    A possibly helpful module for this might be Text::CSV

    Also, you can do it with join as you attempted.

    In your join, you only specified the comma and space:

    join(', ', @list)

    What you left out was including the quotes. There is a bit of a trick as join only puts the supplied string between list members, so you need to provide the first and last quotes yourself.

    '"' . join('", "', @list) . '"'