Another way. Distinguishes undefined from non-existent keys (may be surplus to requirements). Needs 5.10+ for the  // defined-or operator.

c:\@Work\Perl\monks>perl -wMstrict -le "use 5.010; ;; my @AoH = ( { name => 'Adam', age => 0, }, { name => 'Bob', age => 10, }, { name => 'Cat', age => 20, }, { name => '0', age => 30, }, { name => 0, age => 35, }, { name => '', age => 40, }, { name => undef, age => 50, }, { age => 60, }, ); ;; my @empty; ;; print make_csv('name', @AoH); print make_csv('name', @empty); print make_csv('name'); print make_csv(); print make_csv('name', { name => 'Foo', age => 91, }); print make_csv('name', { name => '0', age => 92, }); print make_csv('name', { name => 0, age => 93, }); print make_csv('name', { name => '', age => 94, }); print make_csv('name', { name => undef, age => 95, }); print make_csv('name', { age => 96, }); ;; sub make_csv { my $k = shift; return qq{'@{[ join q{','}, map exists $_->{$k} ? $_->{$k} // '[UNDEF]' +: '???', @_ ]}'}; } " 'Adam','Bob','Cat','0','0','','[UNDEF]','???' '' '' '' 'Foo' '0' '0' '' '[UNDEF]' '???'


Give a man a fish:  <%-{-{-{-<


In reply to Re: Make CSV list from values in AoH by AnomalousMonk
in thread Make CSV list from values in AoH by tel2

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.