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

You need to colour the sentences after you split them into lines. It complicates the code a bit, but it's not that hard: just remember the positions where colours start, unpack the lines, and then compute what line and position the markup belongs to. Note that the positions are sorted in reverse, so that inserting the markup doesn't change the positions to process.

sub wrap { my ($lines, $pos, $markup) = @_; my $idx = int($pos / 50); substr $lines->[$idx], $pos % 50, 0, $markup; } my %dna; my $name1 = "name1"; my $name2 = "name2"; $dna{$name1} = "ATATTATCCCCCTATATATGGAGGGAGAGGGGGGGGGGGGGGGGGGGGGGGGGG +GAGAGAGGAGATTTTTTTTTTTTTTTT"; $dna{$name2} = "ATATATTATATATATTATATTCGCGCGCGCGGCGCGCGCGGCGCGCGCGTTTTT +TTTTTTTTTAGGAGAGAGAGGGAGGAGGAGAGGGGAGT"; my %class =(AGGAG => 'sd', TTTTT => 'terminator'); my $regex = join '|', keys %class; $regex = qr/((?i:$regex))/; my %highlight; for my $name (keys %dna) { while ($dna{$name} =~ /$regex/g) { $highlight{$name}{ pos($dna{$name}) - 5 } = $class{$1}; } } for my $key (keys %highlight) { print "<p> <b>$key</b><input type='radio' name='selected_intergeni +c' value='$dna{$key}'/> </p>"; my @lines = unpack '(A50)*', $dna{$key}; for my $pos (sort { $b <=> $a } keys %{ $highlight{$key} }) { my $class = $highlight{$key}{$pos}; wrap(\@lines, $pos + 5, '</span>'); wrap(\@lines, $pos, "<span class='$class'>"); } print join "<br />", @lines; }

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

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:59 UTC
    Many Thanks this is working and it is exactly what I was trying to do. Many many thanks I have been trying to sort this out for a while now, and I was abou to give up and have a massively wide page. Do you think (be honest) this was a a good question? I could not find a similar question anywhere, and when I have tried asking it on SO, I have been heavily downvoted, criticized and offended. That is why I was also very hesitant into asking here; do not wanna appear as lazy, but I tried all I could think of. I honestly thought this was a decent question for beginners and I tried providing as much material as I could. Thanks again.
Re^2: Print N characters per line in Cgi Script with Html Tags
by Diesel (Novice) on Jun 13, 2017 at 16:23 UTC
    I was wondering: I have noticed you use "5" in your solution; what if the patterns are of different length?
      You can store the lengths in the %class hash:
      my %class =(AGGAG => { name => 'sd' }, TT => { name => 'terminator' }); # I shortened the ter +minator for testing $class{$_}{length} = length $_ for keys %class; # ... $highlight{$name}{ pos($dna{$name}) - $class{$1}{length} } = $ +class{$1}; # ... my $class = $highlight{$key}{$pos}; wrap(\@lines, $pos + $class->{length}, '</span>'); wrap(\@lines, $pos, "<span class='$class->{name}'>");

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
        Thanks; this does the job perfectly. I was trying to do the same by creating another hash, called %size where I was recording the length of the patterns and then use those instead of the "5" but could not make it work. Many thanks again. and also to the other Monks taking the time to reply.