in reply to Re: Make CSV list from values in AoH
in thread Make CSV list from values in AoH

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?

Replies are listed 'Best First'.
Re^3: Make CSV list from values in AoH
by LanX (Saint) on Aug 17, 2018 at 23:27 UTC
    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.