|
Posted by PerlFAQ Server on March 16, 2008, 9:03 am
Please log in for more thread options
This is an excerpt from the latest version perlfaq5.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .
--------------------------------------------------------------------
5.39: Why do I get weird spaces when I print an array of lines?
Saying
print "@lines\n";
joins together the elements of @lines with a space between them. If
@lines were "("little", "fluffy", "clouds")" then the above statement
would print
little fluffy clouds
but if each element of @lines was a line of text, ending a newline
character "("little\n", "fluffy\n", "clouds\n")" then it would print:
little
fluffy
clouds
If your array contains lines, just print them:
print @lines;
--------------------------------------------------------------------
The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.
If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.
|
|
Posted by Ben Bullock on March 16, 2008, 6:18 pm
Please log in for more thread options
On Sun, 16 Mar 2008 06:03:03 -0700, PerlFAQ Server wrote:
> 5.39: Why do I get weird spaces when I print an array of lines?
>
> Saying
>
> print "@lines\n";
>
> joins together the elements of @lines with a space between them.
I recently learnt from a post here by Ben Morrow that it joins together
the elements of @lines with a $" between them:
ben ~ 502 $ perl -e '$"="bu";@ba=("bo","be");print "@ba\n";'
bobube
Shouldn't this FAQ entry mention $"?
|
|
Posted by Uri Guttman on March 16, 2008, 6:49 pm
Please log in for more thread options
BB> On Sun, 16 Mar 2008 06:03:03 -0700, PerlFAQ Server wrote:
>> 5.39: Why do I get weird spaces when I print an array of lines?
>>
>> Saying
>>
>> print "@lines\n";
>>
>> joins together the elements of @lines with a space between them.
BB> I recently learnt from a post here by Ben Morrow that it joins together
BB> the elements of @lines with a $" between them:
BB> ben ~ 502 $ perl -e '$"="bu";@ba=("bo","be");print "@ba\n";'
BB> bobube
BB> Shouldn't this FAQ entry mention $"?
technically you are correct but $" is rarely changed by newbies. its
default value is space so the FAQ addresses that issue. it could be
expanded to mention $" and perlvar but that may be too much info for a
simple FAQ answer.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Architecture, Development, Training, Support, Code Review ------
----------- Search or Offer Perl Jobs ----- http://jobs.perl.org ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
|
|
Posted by Ted Zlatanov on March 17, 2008, 10:46 am
Please log in for more thread options
BB> On Sun, 16 Mar 2008 06:03:03 -0700, PerlFAQ Server wrote:
>>> 5.39: Why do I get weird spaces when I print an array of lines?
>>>
>>> Saying
>>>
>>> print "@lines\n";
>>>
>>> joins together the elements of @lines with a space between them.
BB> I recently learnt from a post here by Ben Morrow that it joins together
BB> the elements of @lines with a $" between them:
BB> ben ~ 502 $ perl -e '$"="bu";@ba=("bo","be");print "@ba\n";'
BB> bobube
BB> Shouldn't this FAQ entry mention $"?
UG> technically you are correct but $" is rarely changed by newbies. its
UG> default value is space so the FAQ addresses that issue. it could be
UG> expanded to mention $" and perlvar but that may be too much info for a
UG> simple FAQ answer.
The problem is that the simple FAQ answer is wrong exactly when people
may be looking for reasons why there are weird separators in a list.
Note also that it doesn't just 'join' the elements but it also prints
them, which is obvious but still incorrect in the FAQ.
I think the FAQ should say:
Saying
print "@lines\n";
prints together the elements of @lines with a space between them (unless
$" has been changed from the default, see `perldoc perlvar').
Ted
|
|
Posted by Michele Dondi on March 17, 2008, 6:08 pm
Please log in for more thread options wrote:
>UG> technically you are correct but $" is rarely changed by newbies. its
>UG> default value is space so the FAQ addresses that issue. it could be
>UG> expanded to mention $" and perlvar but that may be too much info for a
>UG> simple FAQ answer.
>
>The problem is that the simple FAQ answer is wrong exactly when people
>may be looking for reasons why there are weird separators in a list.
Seconded.
>Note also that it doesn't just 'join' the elements but it also prints
>them, which is obvious but still incorrect in the FAQ.
What is "it"? It's not clear from the context, because print() does
print them, but it's up to the double quotes a.k.a. qq() to join
interpolated arrays.
my $s = "@{[qw/foo bar baz/]}";
print $s;
Michele
--
->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
|
| Similar Threads | Posted | | FAQ 6.0: Why do I get weird spaces when I print an array of lines? | December 18, 2004, 6:03 am |
| FAQ 6.0: Why do I get weird spaces when I print an array of lines? | January 1, 2005, 6:03 pm |
| FAQ 6.0 Why do I get weird spaces when I print an array of lines? | February 25, 2005, 6:03 am |
| FAQ 5.38 Why do I get weird spaces when I print an array of lines? | May 8, 2005, 5:03 am |
| FAQ 5.38 Why do I get weird spaces when I print an array of lines? | July 23, 2005, 10:03 pm |
| FAQ 5.38 Why do I get weird spaces when I print an array of lines? | October 9, 2005, 10:03 am |
| FAQ 5.38 Why do I get weird spaces when I print an array of lines? | December 15, 2005, 5:03 pm |
| FAQ 5.38 Why do I get weird spaces when I print an array of lines? | December 22, 2005, 11:03 am |
| FAQ 5.38 Why do I get weird spaces when I print an array of lines? | February 15, 2006, 12:03 pm |
| FAQ 5.38 Why do I get weird spaces when I print an array of lines? | May 4, 2006, 9:03 pm |
|