views:

60

answers:

2

Hi, When I learning to print array variables, I found the white space inserted when double quoter used. Snippet code as below. Could you please tell me why?

#!/usr/bin/perl -w
use strict;
use warnings;

my @str_array = ("Perl","array","tutorial");
my @int_array = (5,7,9,10);

print @str_array;
print "\n";
# added the double quotes
print "@str_array";
print "\n";
print @int_array;
print "\n";
# added the double quotes
print "@int_array";

Output:

Perlarraytutorial
Perl array tutorial
57910
5 7 9 10
+5  A: 

When a variable is used inside a string, string interpolation is used. String interpolation means, among other things, the variable is formatted to become a string. When an array variable is formatted to become a string it is given spaces between the elements. These spaces are actually determined by the variable $". The default value for this variable is a space. For more information see the perl documentation for special variables.

Benson
+5  A: 

Here is the perlfaq that answers your question:

Why do I get weird spaces when I print an array of lines?

codaddict