• Login
Community
  • Login

replace all values of a variable by half

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
3 Posts 2 Posters 877 Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C
    Cynic
    last edited by Oct 1, 2020, 6:49 PM

    is there any way to find and replace all values of a variable with the original value divided by two?

    P 1 Reply Last reply Oct 1, 2020, 7:01 PM Reply Quote 0
    • P
      PeterJones @Cynic
      last edited by PeterJones Oct 1, 2020, 7:03 PM Oct 1, 2020, 7:01 PM

      @Cynic ,

      Not natively: the search-and-replace engine doesn’t handle math.

      However, if you install the PythonScript plugin and look at the documentation, there’s an example of a search-and-replace using PythonScript that adds 1 to the numbers; you would just have to change that example to divide by 2 instead.

      def add_1(m):
          return 'Y' + str(int(m.group(1)) + 1)
      
      # replace X followed by numbers by an incremented number
      # e.g.   X56 X39 X999
      #          becomes
      #        Y57 Y40 Y1000
      
      editor.rereplace('X([0-9]+)', add_1);
      

      becomes

      def div_2(m):
          return 'Y' + str(int(m.group(1)) / 2)
      
      # replace X followed by numbers halved
      # e.g.   X56 X24 X99
      #          becomes
      #        Y28 Y12 Y49
      
      editor.rereplace('X([0-9]+)', div_2);
      
      1 Reply Last reply Reply Quote 2
      • C
        Cynic
        last edited by Oct 1, 2020, 7:03 PM

        thanks a ton!

        1 Reply Last reply Reply Quote 0
        • A Alan Kilborn referenced this topic on May 29, 2022, 6:21 PM
        3 out of 3
        • First post
          3/3
          Last post
        The Community of users of the Notepad++ text editor.
        Powered by NodeBB | Contributors