in reply to Printing Array with quote and comma
One way to do it:
use warnings; use strict; my @arr = qw(book dog cat); print "['", join( "','", @arr), "'];\n";
But I'm curious... why are you trying to reinvent the data dumper wheel?
HTH
Update: As reasonablekeith pointed out, the code above doesn't work as intended for empty arrays. While the map solutions are more elegant (IMO), this corrects the problem:
print "[", ( @arr2 ? "'" . join( "','", @arr2) . "'" : ''), "];\n";
Thanks and ++ for pointing out the error.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Printing Array with quote and comma
by reasonablekeith (Deacon) on Oct 05, 2005 at 10:00 UTC |