Printing an array in double quotes

my @a = (1,2,3,4,5);

print @a;      #output: 12345

print "\n";

print "@a";    #output: 1 2 3 4 5

Printing an array by putting its name with double quotes puts a space between each index value in the output. How it's done? Why print @a;doesn't it print the same? What is the need for both types? I mean when you will use print @a;instead print "@a";and vice versa.

+5
source share
5 answers

An even better question is: why doesn't it print something like an array {0x1232ef}. Printing should print the output of a string, @anot a scalar.

Hell, even better: this is a scalar context, so why not print 5, which is the number of elements in the array. Here's how to do it:

print scalar @a;

.

print , , , , , .

:

@a = qw(a b c d e);

print "@a";         #prints "a b c d e"
print "\n";

print @a;           #prints "abcde"
print "\n";

print @a . "\n";    #prints "5"

print scalar @a;    #prints "5"

, print @a abcde, \n , @a .

Perldoc ( perldoc -f print. Perl perldoc)

* print LIST
* print

Prints a string or a list of strings. Returns true if successful[...]

! , .

$, ( ) LIST. $\ ( ) . print LIST, LIST , , . ​​

:

@a = qw(a b c d e);

$, = "--";
print "@a";         #prints "a b c d e"
print "\n";

print @a;           #prints "a--b--c--d--e"
print "\n";

print @a . "\n";    #prints "5"

print scalar @a;    #prints "5"

... $, , @a . , $, perldoc, why is everyone prattling about $"`?

perldoc perlvar

* $LIST_SEPARATOR
* $"

When an array or an array slice is interpolated into a double-quoted string or a
similar context such as /.../ , its elements are separated by this value. Default
is a space. For example, this:

print "The array is: @array\n";

is equivalent to this:

print "The array is: " . join($", @array) . "\n";

Mnemonic: works in double-quoted context.

, !

$" - , $, null. , !

...

@a = qw(a b c d e);

$, = "--";
$" = "++";
print "@a";         #prints "a++b++c++d++e"
print "\n";

print @a;           #prints "a--b--c--d--e"
print "\n";

print @a . "\n";    #prints "5"
print scalar @a;    #prints "5"
+9

, , ; , , , . ; $, ( '') , $" ( '') , .

+9

print, .

, , . , $", .

+6

Perl , , join:

print "@array";

print join($" => @array);

$" .

, $"

print "@array";     # '1 2 3 4 5'
{
    local $" = '';  # "
    print "@array"; # '12345'
}
print "@array";     # '1 2 3 4 5' 

, Perl -q B::Deparse:

$ perl -MO=Deparse,-q -e 'print "@array"'
print join($", @array);
-e syntax OK
+1

" " perldata , "@a":

, , $" ($LIST_SEPARATOR, use English;), . :

$temp = join($", @ARGV);  # " for benefit of StackOverflow hiliter
system "echo $temp";
system "echo @ARGV";

, print "@a" print. , print @a .

perlfunc print , print .

print LIST

... $, ( ) LIST.

perlvar $,

Handle->output_field_separator( EXPR )
$OUTPUT_FIELD_SEPARATOR
$OFS
$,

. , . undef.

: , print ,.

$, undefined, print .

print join("", @a);

?

print. . , Google print "@... , print "@foo" - ,

sub foo {
  print "args = @_\n" if $debug;
  ...;
}

A handy trick that Mark Dominus uses to provide visible delimiters for lists whose values ​​may contain spaces,

sub foo {
  if ($debug) {
    local $" = "][";
    print "args = [@_];  # e.g., [foo][1][3.14159]
  }
  ...;
}

Say @acontains lines of output such as

@a = map "  - $_\n", @error_messages;
print @a;

Inserting implicit spaces between values ​​will result in my carefully-designed formatting:

  - sneaky-sneaky, sir
   - the hideousness of that foot will haunt my dreams forever
    - why would you do that ?!

Perl tries to do what we mean, even if we do not agree with the requirements that we make.

+1
source

All Articles