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


In reply to Re^3: Quoting each word in an arrayref by haukex
in thread Quoting each word in an arrayref by soundX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.