Print in Perl shows leading spaces for each item.

I upload a file to an array (each line in an array element). I process the elements of the array and save it in a new file. I want to print a new file:

print ("Array: @myArray");

But - he shows them with leading spaces in each line. Is there an easy way to print an array without initial spaces?

+3
source share
2 answers

Matt Fenwick is true. When your array is in double quotation marks, Perl will put a value $"(by default it is a space, see man perlvar ) between the elements. You can simply put it outside the quotation marks:

print ('Array: ', @myArray);

If you want the elements to be separated, for example, with a comma, change the output field separator:

use English '-no_match_vars';
$OUTPUT_FIELD_SEPARATOR = ',';     # or "\n" etc.
print ('Array: ', @myArray);
+4
source

- join:

my $delimiter = '';  # empty string

my $string = join($delimiter, @myArray);

print "Array: $string";
+6

All Articles