Hello fellow Monks!
I am trying to optimize a code that I have, which is supposed to do the following:
If you have 2 strings, like the following:
$str_no_dash=' IIIIIIIIIIIIIIIIIIIIIIIIIIIMMMMMMMOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOMMMMMM +MMMIIIMMMMMMMMMMMOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOMMMMMMMMMI +IIMMMMMMMMMOOOOMMMMMMMMMIIMMMMMMMMMOOOOOOOOOOOOOOOOOMMMMMMMMMIIIIMMMM +MMMMMOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOMMMMMMMIIIIIMMM +MMMMMMMMOOOOOOOOOOOOOOOOOOOMMMMMMMMMMMIIIIIMMMMMMMMMMMOOOOOOOOOOOO'; $str_with_dash='VNRV-L-K--R-------PL------A-------N---------PV----N--- +--D----V--G---V--T-----L-----G------T-------------------------------- +--------------------------------------------------------------------- +--------------------------------------------------------------------- +---RG----E--R----RG--E-F-----D-L--G----WN----A----N-------D---------- +----------A-------AR-----F---RM----T----G-----A-----A--E-N----S-N-SF- +--------------------------------------------------------------------- +--------------------------------------------------------------------- +--------------------------------------------------------------------- +-----------------------------------------------R--------------------- +--------------------------------------------------------------------- +--------------------------------------------------------------------- +------------------------------------DQF--Q-----L-N-R-Q---A----IA--P-- +S----------------AQ--F---------K-------L----D---R----------D--------T +--V--L--------N--V-------E--FDY-LHD---------------------------------- +--------------------------------------------------------------------- +------------------------------------------------------R--R---T--S---D +--Q--GI-------------------------------------P--AYR------------------- +------------------GRPV---------------DVP-IN----TY-Y-GSAD------------- +----------------------------------------------------------------GVNSS +YNDV-S----A-K--SA-----------------T-V--------S----L-------------D---- +-H-----R---------------F----N-----------D-S-L-S-F------------------HG +--A------------------------------------------------------------------ +-------------------------------------------I--R--AY------------------ +--------DF--SL------ER----------------------------------------------- +-----K-N---Y-V-----T----Y----E--P-I---K-TA--------------------------- +--------------------------------------------------------------------- +-----------------------------------------------AHP--------------VV--T +--L--D-Q--S--T------------------------------------------------------- +--------------------------------------------------------------------- +--------------------------------------------------------------------- +--------------------------------------------------------------------- +RQRTDH-----G--I-D---G--L--F--EL----------------------------------T--- +-QK---TS-----------LFGM---------------------------------------------- +--------------------------RH--E----------L---L--------Y------G--L---- +---E--LS--Q----Q--Q-K------------------------------------------------ +--------------------------------------------------------------------- +--------------------------------------------------------------------- +--------------------------------------------------------------------- +---------------------F--DTIY--------S---------------V-S-------------- +---------------------------------------------------------------K---VA +-TYDLFNP----------------------------------QPVVLPG--VP----------TGT--- +--------------------------------------------------------------------- +--------------------------------------------------------------------- +----------RA----K-----------------------TNASTVVG-LA-----G--V-Y----A-- +------Q-------D--L--IS-----------------------L---------T---E--H------ +-------W---K-----V---L--A---G----------------------------L----------- +--R---F-------------------------------------DY---------------------L- +-------------------------------NQ-------------I---------------------- +RHDYTSSNV------------------------------------------------------------ +--------------------------------------------------------------------- +--------------------------------------------------------------------- +--------------------------------------------------------------------- +--------------------------NLDRTDHAW----SP-------R---V---------------- +-------------------G--LI------Y-------------------------------E--P--- +----L---DW----------------------------------L--TL--Y----G--SFSQ------ +-----S-F------S-------------------------------------P-L--A----------- +--D----T----L-------I-S--SG------------A----------------------------- +---------------------------------------------------------------';

to create a new string, which will have the same letters as $str_no_dash, but with - in the same positions as $str_with_dash.
The thing is that I can do it with split but I think there could be a faster solution using for loop...Can you show me how?
@final=(); @split_str_no_dash = split(//, $str_no_dash); @split_str_with_dash = split(//, $str_with_dash); $count_pos = -1; $count_pred = -1; @str_final=(); foreach $b(@split_str_with_dash) { if ($b=~/\./ or $b=~/-/) { $count_pos++; $str_final[$count_pos]=$b; } elsif ($b!~/\./ && $b!~/-/) { $count_pos++; $count_pred++; $str_final[$count_pos]=$split_str_no_dash[$count_pred]; } } $final_string=join("", @str_final); }

In reply to How can I do this action WITHOUT split? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.