How to batch out lines of code with same code but different variables?



  • I’m working on macros for a maptool campaign and one thing that keeps getting me is that I have to make multiple(40+) lines of the same code with only minor changes in the variables, while the rest of the code stays the same. Is there a way to insert a list of variables and have notepad++ substitute the second variable for all of the occurrences of the first variable in the second line, then the third for the third line etc…
    Example:
    Line 1:"aAppraise|0|Appraise “+Appraise+”/“eval(+mAppraise+)” “+cAppraise+”,
    Line 2:"aBluff|0|Bluff “+Bluff+”/“eval(+mBluff+)” “+cBluff+”,
    Line 3: (replace all instances of Bluff with Concentration)

    I know I can go line by line with find and replace, but this is pretty tedious as well.



  • Hello, @shadowfire-omega and All,

    Again, with a S/R using regular expressions, there’s a solution ;-))

    • Organize all the names of the variables as a list, one per line, beginning with a separator character, not already used in your text ( For instance, the #, the = or the @ symbol )

      • Choosing the equal sign as a separator, here is, below, a sample list of 8 names :
    =Appraise
    =Bluff
    =Concentration
    =Test
    =Shadowfire
    =Omega
    =OK
    =The End
    
    • Then, using the column mode ( or rectangular selection ), copy the selection of names in the clipboard

    • Now, recopy, with the Ctrl + D command, the first line which is to be changed, as many times, than the number of lines of the previous list ( 8 ), as a template, giving :

    "aAppraise|0|Appraise "+Appraise+"/"eval(+mAppraise+)" "+cAppraise+"
    "aAppraise|0|Appraise "+Appraise+"/"eval(+mAppraise+)" "+cAppraise+"
    "aAppraise|0|Appraise "+Appraise+"/"eval(+mAppraise+)" "+cAppraise+"
    "aAppraise|0|Appraise "+Appraise+"/"eval(+mAppraise+)" "+cAppraise+"
    "aAppraise|0|Appraise "+Appraise+"/"eval(+mAppraise+)" "+cAppraise+"
    "aAppraise|0|Appraise "+Appraise+"/"eval(+mAppraise+)" "+cAppraise+"
    "aAppraise|0|Appraise "+Appraise+"/"eval(+mAppraise+)" "+cAppraise+"
    "aAppraise|0|Appraise "+Appraise+"/"eval(+mAppraise+)" "+cAppraise+"
    
    • Then, placing the cursor at the end of the first line, simply, paste the rectangular selection. You should obtain the following list :
    "aAppraise|0|Appraise "+Appraise+"/"eval(+mAppraise+)" "+cAppraise+"=Appraise
    "aAppraise|0|Appraise "+Appraise+"/"eval(+mAppraise+)" "+cAppraise+"=Bluff
    "aAppraise|0|Appraise "+Appraise+"/"eval(+mAppraise+)" "+cAppraise+"=Concentration
    "aAppraise|0|Appraise "+Appraise+"/"eval(+mAppraise+)" "+cAppraise+"=Test
    "aAppraise|0|Appraise "+Appraise+"/"eval(+mAppraise+)" "+cAppraise+"=Shadowfire
    "aAppraise|0|Appraise "+Appraise+"/"eval(+mAppraise+)" "+cAppraise+"=Omega
    "aAppraise|0|Appraise "+Appraise+"/"eval(+mAppraise+)" "+cAppraise+"=OK
    "aAppraise|0|Appraise "+Appraise+"/"eval(+mAppraise+)" "+cAppraise+"=The End
    
    • Finally, open the Replace panel ( Ctrl + H )

    • Select the Regular expression search mode

    • In the Find what: and Replace with: zones, type the regexes below :

    SEARCH (?-is)Appraise(?=.+=(.+))|=.+

    REPLACE ?1\1

    • Click on the Replace button, as many times as necessary

    Et voilà ! You’ll get the expected text, below :

    "aAppraise|0|Appraise "+Appraise+"/"eval(+mAppraise+)" "+cAppraise+"
    "aBluff|0|Bluff "+Bluff+"/"eval(+mBluff+)" "+cBluff+"
    "aConcentration|0|Concentration "+Concentration+"/"eval(+mConcentration+)" "+cConcentration+"
    "aTest|0|Test "+Test+"/"eval(+mTest+)" "+cTest+"
    "aShadowfire|0|Shadowfire "+Shadowfire+"/"eval(+mShadowfire+)" "+cShadowfire+"
    "aOmega|0|Omega "+Omega+"/"eval(+mOmega+)" "+cOmega+"
    "aOK|0|OK "+OK+"/"eval(+mOK+)" "+cOK+"
    "aThe End|0|The End "+The End+"/"eval(+mThe End+)" "+cThe End+"
    

    By this means, you can customize any number of lines, at once !!


    Of course, if you used, for instance, the @ separator, instead of the = sign, you must modify the regex as below :

    SEARCH (?-is)Appraise(?=.+@(.+))|@.+

    And if you used the # symbol, the search regex is changed into :

    SEARCH (?-is)Appraise(?=.+#(.+))|#.+

    Best Regards,

    guy038



  • Works like a charm! Already did the long way, but still have at least 10 more operations like this to deal with for this framework, so thank you big time!



  • Hello, @shadowfire-omega,

    Just explain me the various operations that you need to perform, and maybe, some of them could be gathered in a same regex S/R ;-))

    And, of course, I don’t mind explain my previous regex , if you would like to !

    See you later,

    Cheers,

    guy038



  • Thank you for the offer, but using what you have provided me is all I currently need. But if you can explain how it works it would be appreciated!



  • Hi, @shadowfire-omega and All,

    Hope you don’t mind my late reply ! So, given the 8 lines "aAppraise|0|Appraise "+Appraise+"/"eval(+mAppraise+)" "+cAppraise+", which end, each, with a word, after the = separator sign

    "aAppraise|0|Appraise "+Appraise+"/"eval(+mAppraise+)" "+cAppraise+"=Appraise
    "aAppraise|0|Appraise "+Appraise+"/"eval(+mAppraise+)" "+cAppraise+"=Bluff
    "aAppraise|0|Appraise "+Appraise+"/"eval(+mAppraise+)" "+cAppraise+"=Concentration
    "aAppraise|0|Appraise "+Appraise+"/"eval(+mAppraise+)" "+cAppraise+"=Test
    "aAppraise|0|Appraise "+Appraise+"/"eval(+mAppraise+)" "+cAppraise+"=Shadowfire
    "aAppraise|0|Appraise "+Appraise+"/"eval(+mAppraise+)" "+cAppraise+"=Omega
    "aAppraise|0|Appraise "+Appraise+"/"eval(+mAppraise+)" "+cAppraise+"=OK
    "aAppraise|0|Appraise "+Appraise+"/"eval(+mAppraise+)" "+cAppraise+"=The End
    

    and the regex S/R :

    SEARCH (?-is)Appraise(?=.+=(.+))|=.+

    REPLACE ?1\1

    • First, the (?-is) forces the regex search to be sensitive ( non-insensitive !) to the case and the dot . regex character to represents any single standard character and not an End of Line character

    • Then it matches any of the two alternatives, giving priority to the first one

      • The regex (?-is)Appraise(?=.+=(.+))

      • The regex =.+

    • If the first alternative is chosen, it, then, matches the Appraise string but only if the look-ahead feature is verified. That is to say if there is, further on, in the same line, a = sign, followed by some standard characters, which ends the current line. As this condition is, by construction, always true, the part after the = sign is, thus, stored as group 1

    As soon as the = location is reached, by the regex engine, no more = sign can be found, afterwards. So, the first alternative cannot be matched, anymore, in current line !

    • But, now, the second alternative =.+ matches all the end of line, beginning with the = sign

    • In replacement, ?1\1 is a conditional structure, which means :

      • If group 1 exists, we replace the string Appraise with the contents of group 1

      • If group 1 does not exist, we do not rewrite anything. So, the range of characters beginning with the = sign till the end of current line, is deleted

    Et voilà !

    Cheers

    guy038


Log in to reply