CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Advanced:
perl print adds unwanted leading whitepsace


New User

Mar 26, 2008, 12:41 AM

Post #1 of 2 (1425 views)
perl print adds unwanted leading whitepsace Can't Post

The following bit of code adds unwanted whitespace at the beginning of some lines. It looks like the /n in the added text is not interpret correctly. Using komodo debugger I cannot pinpoint the problem. The generic array is filled correctly, only after the print the output is incorrect.
See code and output:

my @generic; 
my @allGeneric;

# read some lines from file, no specific mark up
# store matching lines in @genreric
FH = open "<", "Some file.txt";

while (<FH>)
if ($line =~ /some match/)
push (@generic, $line);
close FH;

#add some trailing text
#remove first element since it si always empty due to push

push (@generic, "\nSee some place: \n");
splice (@generic, 0,1);
push (@allGeneric,"*"x80 . "\n");
push (@allGeneric,@generic);

print "The system folder is ...\n\n";
print "===== Generic actions =====\n";
print "@allGeneric\n";

This produces

The system folder is ...

===== Generic actions =====
Ja, we hebben hier een heleboel werk om alles goed te krijgen met allerlei linefeeds en line dingetjes waar ik niet zo'n goed zicht op heb in perl gebruik. Daarom is het af en toe ook wel lastig, maar toch ook wel (heel) erg vaak.

En soms: treedt er een 'panic' of "dubbele quote" op, maar mag dat?

See some place:

Note the leading whitespace before 'Ja, ' and before 'En soms'.
(In preview post the leading whitespaces seem to have been removed... In hex editor this space is present as a 0x20, so a real space.)

Any help?

(This post was edited by rroo on Mar 26, 2008, 12:44 AM)


Mar 26, 2008, 9:37 AM

Post #2 of 2 (1416 views)
Re: [rroo] perl print adds unwanted leading whitepsace [In reply to] Can't Post

You are printing an array in scalar context:

print "@allGeneric\n";

this adds a space between each element of the array. This behavior is well known and documented in the perl documentation.

change to:

print "$_\n" for @allGeneric;

and see if that helps.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives