How can I output every element of a Perl array surrounded by quotation marks?

I want to output array elements in a specific format in Perl.

@myArray = ("A", "B", "C");
$text = something;

Something must be a string ' "A" "B" "C"' (each element is enclosed in double quotes).

However, if @myArrayempty, then $textit should be. I was thinking about using join()for example

$text = "\"" . join("\" \"", @myArray) . "\"";
if ($text eq "\"\"")
{
    $text = "";
}

I think this will work. However, is there a more elegant way to do this?

+5
source share
4 answers

Use map:

#!/usr/bin/perl

use strict;
use warnings;

my @a    = qw/ A B C /;
my @b;
my $text = join ' ', map { qq/"$_"/ } @a;
print "text for (@a) is [$text]\n";

$text = join ' ', map { qq/"$_"/ } @b;
print "text for (@b) is [$text]\n";

, , qq// ( , "", ), " s.

+26

. , $", :

my $text = do { local $" = q<" ">; qq<"@array"> };
+4

"join". ? , ,

my @a = qw|a b c|;
@a = map {qq|"$_"|} @a;    

, , . , , DBI, , :

@a = map{$dbh->quote($_)} @a;

,

+1

, , , Data:: Dumper .

-2
source

All Articles