Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Print N characters per line in Cgi Script with Html Tags

by hippo (Bishop)
on Jun 13, 2017 at 15:07 UTC ( [id://1192700]=note: print w/replies, xml ) Need Help??


in reply to Print N characters per line in Cgi Script with Html Tags

If I understand you correctly, all you need is something like this:

#!/usr/bin/env perl use strict; use warnings; my $in = "ATATATTATATATATTATATTCGCGCGCGCGGCGCGCGCGGCGCGCGCGTTTTTTTTTTT +TTTAGGAGAGAGAGGGAGGAGGAGAGGGGAGT"; my $out = '<pre>' . join ("\n", $in =~ /[ATCG]{1,50}/g) . "</pre>\n"; print $out;

Use HTML pre tags for preformatted output.

Replies are listed 'Best First'.
Re^2: Print N characters per line in Cgi Script with Html Tags
by Diesel (Novice) on Jun 13, 2017 at 15:24 UTC
    this does not work; lines end up being of different length and the colours disappear. To simplify my task to the minimum, imagine, still inside a CGI script you have 2 strings:
    my $a: "ABCDEFGHI" my $b: "<b>ABC</b>DEFGHI"
    I want to print this 2 strings with 4 characters per line and in the second string ABC has to be in bold; If I use my method, or your method, this is what I get:
    $a: ABCD EFGH I $b: <b>A BC</ b>DE FGHI
    I want the bold tags ingored in the characters counts: So:
    $a: ABCD EFGH I $b: <b>ABC</b>D EFGH I
    Also as I said, when I try your code it also does something with the coloured tags I used; Note: in this example I am using bold tag to make it shorter, in my scrip I am using a span class as shown. I hope it is more clear. I do not know in advance where the position it is: i need an universal solution which can print something with the same number of characters per line, basically ignoring the tags in the counts.

      I see. If you have tags already in your data and want to skip over them then the best plan is to use a proper HTML parser.

      If you cannot (or will not) use a parser then a crude plan B which works for your supplied dataset is:

      #!/usr/bin/env perl use strict; use warnings; use Test::More; my @set = ( { in => 'ABCDEFGHI', want => "ABCD\nEFGH\nI\n" }, { in => '<b>ABC</b>DEFGHI', want => "<b>ABC</b>D\nEFGH\nI\n" }, ); plan tests => scalar @set; my $len = 4; for my $x (@set) { my $i = 0; my $out = ''; my $intag = 0; for my $c (split (//, $x->{in})) { $out .= $c; $intag++ if $c eq '<'; $intag-- if $c eq '>'; next if $intag || $c eq '>'; $i++; $out .= "\n" unless $i % $len; } $out .= "\n"; is ($out, $x->{want}); }

      This isn't robust (and is rather C-ish for my taste) but it serves to illustrate this approach in general terms. Have fun with it.

      Update: edited source for improved generality.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1192700]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2024-04-24 12:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found