in reply to Make CSV list from values in AoH

you may want to try something like

  $csv  = join "," , map { "'$_'" }  map { $AoH[$_]{name} } 0 .. $#AoH;

update

maybe clearer are two lines

push @names, $_->{name} for @AoH; $csv = join "," , map { "'$_'" } @names;

update
thinking about it, I'd personally prefer :)

$csv = join "," , map { "'$_'" } map { $_->{name} } @AoH;

of course you are free to merge the two maps into one :)

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Replies are listed 'Best First'.
Re^2: Make CSV list from values in AoH
by tel2 (Pilgrim) on Aug 17, 2018 at 23:13 UTC
    Thanks very much, Rolf.

    All your solutions seem to work to my original spec's, thanks!

    Sorry to report that I forgot to mention that if there are no names, the output should be 2 single quotes (see update in my first post).

    I've come up with this, which is based on your 1st solution:
        $csv = "'" . (join "','", map{$_} map{$AoH[$_]{name}} 0..$#AoH) . "'";
    or more simply:
        $csv = "'" . (join "','", map{$AoH[$_]{name}} 0..$#AoH) . "'";
    or even:
        $csv = "'" . (join "','", map{$_->{name}} @AoH) . "'";

    BTW, how do I merge the 2 maps you mentioned into 1?

      Your approach is not advisable.

      Just imagine you need to change the quote from ' to ", then you'll have to change 4 code places instead of 2.

      depends what "if there are no names" means.

      • the key "name" is missing
      • the value for "name" is ""
      • the value for "name" is undef

      map{ $AoH[$_]{name} || "" } should cover all cases except if the name "0" is not allowed (untested)

      good night! :)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        Thanks Rolf.

        Good point - sorry for the ambiguity.  Basically meant the key "name" is missing.  Actually, the @AoH will be empty to there won't be any keys. I'm not sure how I can use:

        map{ $AoH$_{name} || "" }
        in context.  Is that what you meant by merging the two maps into one?

        But I'm pretty happy with the other stuff you've given me, thanks for your help.  Good morning...from NZ.