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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Printing a fixed number of characters in CGI script

 



Peter
Novice

Mar 19, 2016, 4:20 PM

Post #1 of 3 (1186 views)
Printing a fixed number of characters in CGI script Can't Post

How do I go about printing the contents of a variable in a way that prints an arbitrary/fixed amount of characters per line?
I.e. if the variable $seq contains ABCDEFGHIJ how do I get perl to print 3 characters per line as such:
ABC
DEF
GHI
J
The following script works well from the command line, but I can't get it to work through my CGI script (1).
The way I usually print variables in CGI (2).
I reckon the $_\n is rendered ineffective so I tried (3), but it just prints 1 line.
Maybe there's another way?


Code
 
#(1)
my $string = join( '', 'A' .. 'J') x 10;

print "$_\n" for unpack '(A10)*', $string;

#(2)

my $string = join( '', 'A' .. 'J') x 10;

print "<p>"$_\n" for unpack '(A10)*', $string</p>";

#(3)

my $string = join( '', 'A' .. 'J') x 10;

print "$_\n" for unpack '(A10)*',"<p> $string</p>";


(This post was edited by Peter on Mar 19, 2016, 4:22 PM)


BillKSmith
Veteran

Mar 19, 2016, 8:01 PM

Post #2 of 3 (1177 views)
Re: [Peter] Printing a fixed number of characters in CGI script [In reply to] Can't Post

The newline characters are ignored by your browser. You need tags around each field to tell the browser to display it as a separate line. Add the tags to your method (1).

Code
print "<p>$_</p>\n" for unpack '(A10)*', $string;

Good Luck,
Bill


Peter
Novice

Mar 20, 2016, 2:33 AM

Post #3 of 3 (1175 views)
Re: [BillKSmith] Printing a fixed number of characters in CGI script [In reply to] Can't Post

It works. Thanks very much Bill! Wink

 
 


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

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