Perl print shows leading spaces for every element

7.7k views Asked by At

I load a file into an array (every line in array element). I process the array elements and save to a new file. I want to print out the new file:

print ("Array: @myArray");

But - it shows them with leading spaces in every line. Is there a simple way to print out the array without the leading spaces?

2

There are 2 answers

4
Konerak On BEST ANSWER

Matt Fenwick is correct. When your array is in double quotes, Perl will put the value of $" (which defaults to a space; see the perlvar manpage) between the elements. You can just put it outside the quotes:

print ('Array: ', @myArray);

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

use English '-no_match_vars';
$OUTPUT_FIELD_SEPARATOR = ',';     # or "\n" etc.
print ('Array: ', @myArray);
1
Matt Fenwick On

Yes -- use join:

my $delimiter = '';  # empty string

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

print "Array: $string";