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?
Yes -- use join
:
my $delimiter = ''; # empty string
my $string = join($delimiter, @myArray);
print "Array: $string";
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With