Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

How do I put bracket on substring(s) of a string?

( #487086=categorized question: print w/ replies, xml ) Need Help??
Contributed by monkfan on Aug 27, 2005 at 03:19 UTC
Q&A  > strings


Description:

I am trying to put the bracket in a string given the set of its substrings. Like this:
String Substrings Desired Result 1.CCCATCTGTCCTTATTTGCTG ATCTG ATTTG CCC[ATCTG]TCCTT[ATTTG]CTG 2.ACCCATCTGTCCTTGGCCAT CCATC AC[CCATC]TGTCCTTGGCCAT 3.CCACCAGCACCTGTC CCACC CCAGC GCACC [CCACCAGCACC]TGTC 4.CCCAACACCTGCTGCCT CCAAC ACACC C[CCAACACC]TGCTGCCT

Answer: How do I put bracket on substring(s) of a string?
contributed by monkfan

This is an attempt to save a thread in SoPW which I thought can be useful for others.

There are already incredible answers in the thread, so feel free to check in SoPW. Please don't misunderstand me. I honestly think this can be useful. I don't mean to boost my XP here, or in that thread which I posted.

One possible answers is given by good sgifford

#!/usr/bin/perl -w use strict; my $s1 ='CCCATCTGTCCTTATTTGCTG'; my @a1 = qw(ATCTG ATTTG); my $s2 ='ACCCATCTGTCCTTGGCCAT'; my @a2 = qw(CCATC); my $s3 ='CCACCAGCACCTGTC'; my @a3 = qw(CCACC CCAGC GCACC); my $s4 ='CCCAACACCTGCTGCCT'; my @a4 = qw(CCAAC ACACC); put_bracket($s1,\@a1); put_bracket($s2,\@a2); put_bracket($s3,\@a3); put_bracket($s4,\@a4); sub put_bracket { my( $str, $ar ) = @_; foreach my $subs ( @$ar ) { # Construct a regexp with [\[\]] between all the letters my $newsub = join '[\[\]]?', split //, $subs; $str =~ s/($newsub)/[$1]/g; } # Now de-nest the brackets in the string my $depth = 0; my $newstr = ''; foreach my $c ( split //, $str ) { if ( $c eq '[' ) { $newstr .= $c if $depth++ == 0; } elsif ( $c eq ']' ) { $newstr .= $c if --$depth == 0; } else { $newstr .= $c; } } return $newstr; }
More can be found here. Don't vote my root posting but vote those remarkable answers you will find there.
Answer: How do I put bracket on substring(s) of a string?
contributed by Roy Johnson

#!perl use strict; use warnings; my @testdata = (['CCCATCTGTCCTTATTTGCTG', [qw(ATCTG ATTTG)]] ,['ACCCATCTGTCCTTGGCCAT', [qw(CCATC)]] ,['CCACCAGCACCTGTC', [qw(CCACC CCAGC GCACC)]] ,['CCCAACACCTGCTGCCT', [qw(CCAAC ACACC)]]); for (@testdata) { my ($str, $pats) = @$_; print put_bracket($str, @$pats), "\n"; } sub put_bracket { my $str = shift; # Combine multiple match strings into alternations my $rx = join '|', @_; my @brackets; # Store bracket points for every match while ($str =~ /(?=($rx))/g) { push(@brackets, [$-[0], length($1)+$-[0]]); } # Condense overlapping brackets for my $i (0..$#brackets-1) { if ($brackets[$i][1] >= $brackets[$i+1][0]) { $brackets[$i+1][0] = $brackets[$i][0]; @{$brackets[$i]} = (); } } # Apply the brackets (from back to front) while (@brackets) { my $b = pop @brackets; next unless @$b; substr($str, $b->[1], 0) = ']'; substr($str, $b->[0], 0) = '['; } return $str; }
Answer: How do I put bracket on substring(s) of a string?
contributed by jdporter

sub put_brackets { my( $str, @pats ) = @_; my $mask = ' ' x length $str; my $ofs = 0; for ( @pats ) { $ofs = index $str, $_, $ofs; last if $ofs < 0; # this pattern not found; look for no more. substr $mask, $ofs, length($_), 'a' x length($_); } # print "string= '$str'\n"; # print "mask = '$mask'\n"; join '', map sprintf( /a/ ? '[%s]' : '%s', substr $str, 0, length($_), '' ), split /\b/, $mask }

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (4)
    As of 2014-09-21 03:48 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (166 votes), past polls