Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl print shows leading spaces for every element

Tags:

arrays

perl

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?

like image 915
Eldad Assis Avatar asked Dec 16 '22 01:12

Eldad Assis


2 Answers

Yes -- use join:

my $delimiter = '';  # empty string

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

print "Array: $string";
like image 118
Matt Fenwick Avatar answered Jan 10 '23 19:01

Matt Fenwick


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);
like image 32
Konerak Avatar answered Jan 10 '23 19:01

Konerak