tel2 has asked for the wisdom of the Perl Monks concerning the following question:
Dearest Monks,
'Adam','Bob','Cat'
can anyone offer more concise/efficient/elegant ways of doing it than this kind of approach, which does work, and is pretty concise, but ain't pretty:
Let's say I've got an array of hashes like this:
and I want to produce a 'quoted' comma separated list (a bit like CSV) of the names, i.e.:@AoH = ( { name => Adam, age => 0 }, { name => Bob, age => 10 }, { name => Cat, age => 20 } );
'Adam','Bob','Cat'
can anyone offer more concise/efficient/elegant ways of doing it than this kind of approach, which does work, and is pretty concise, but ain't pretty:
Order of names doesn't matter.$csv .= ",'$AoH[$_]{name}'" for (0..$#AoH); $csv =~ s/^,//;
Update: If there are no names (i.e. @AoH is empty), the output should be:
''
i.e. 2 single quotes, but my solution above doesn't meet that requirement (I've replied to Rolf below with some which do, though).
Thanks.
tel2
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Make CSV list from values in AoH
by kevbot (Vicar) on Aug 18, 2018 at 06:44 UTC | |
by tel2 (Pilgrim) on Aug 20, 2018 at 01:27 UTC | |
Re: Make CSV list from values in AoH
by kcott (Archbishop) on Aug 18, 2018 at 05:35 UTC | |
by tel2 (Pilgrim) on Aug 20, 2018 at 01:05 UTC | |
Re: Make CSV list from values in AoH
by AnomalousMonk (Archbishop) on Aug 18, 2018 at 07:17 UTC | |
by tel2 (Pilgrim) on Aug 20, 2018 at 02:07 UTC | |
by AnomalousMonk (Archbishop) on Aug 20, 2018 at 05:23 UTC | |
by tel2 (Pilgrim) on Aug 20, 2018 at 22:17 UTC | |
Re: Make CSV list from values in AoH
by LanX (Saint) on Aug 17, 2018 at 22:43 UTC | |
by tel2 (Pilgrim) on Aug 17, 2018 at 23:13 UTC | |
by LanX (Saint) on Aug 17, 2018 at 23:27 UTC | |
by tel2 (Pilgrim) on Aug 17, 2018 at 23:40 UTC | |
Re: Make CSV list from values in AoH
by Tux (Canon) on Aug 19, 2018 at 12:47 UTC |
Back to
Seekers of Perl Wisdom