Find-Replace where Replacing with a Variable Help



  • Yeah a python script would be a good idea. I’ll need to brush off my python books though. Any good tutorials for using python with Np++ off the top of your head? I only script with Lua really, and first programming language was C, so I kinda skipped python.

    The records aren’t actually named anything different. That was just for the example. So it would have to be based on the line number or order it was found.



  • If you are familiar with Lua I would propose you install Lua script plugin,
    which wraps the scintilla a notepad functions. Currently it looks like the download is broken but I assume @dail will fix this soon.

    If python should be used anyway, then I would propose to install the msi and documentation is provided here.

    If there is any question in regards how to use or how to solve, let us know.

    Cheers
    Clauida



  • What exactly is broken?



  • Hi dail,
    when trying to download the precompiled plugin you get an access denied message,
    Source can be downloaded.

    Cheers
    Claudia



  • Well darn. Might be a few days till I can check into it.



  • Hi dail and all,

    it is working - problem was my firefox configuration which doesn’t allow cross site scripting.
    Once I allowed github-cloud.s3.amazonaws.com, dll could be downloaded. Sorry for confusing.

    Cheers
    Claudia



  • Well thanks all for the advice. I chose to try scripting it in Python because I’m always keen to learn something new and Python seems nice and easy for such a simple job. Somewhat expectedly my first attempt hasn’t worked. I’m not entirely sure I’m using the functions right and I don’t want anybody to write the script for me but any help pointing me in the right direction would be appreciated. It’s very self-explanatory - the script finds the specified field and changes its value and adding an ‘f’ at the end to indicate a float to the parser. It skips a number of records at the beginning of the file signified by ignore_records, setting those to just initial_value without adding on delta_value. When I run it however, I get a notification saying “Unknown exception” and nothing happens.

    field = “foo”
    initial_value = 1
    delta_value = 0.05
    ignore_records = 3
    record_n = 0

    Editor.gotoLine(0)
    Editor.searchAnchor()

    while Editor.gotoPos( Editor.searchNext(0,field) ) != -1
    record_n += 1
    if record_n > ignore_records
    initial_value += delta_value
    Editor.setSel( Editor.getAnchor + len(field), Editor.getLineEndPosition() )
    Editor.replaceSel( str(initial_value) + ‘f’ )



  • The “Unknown exception” error indicates that something went wrong during install of the plugin.
    Could it be, that you used the plugin manager instead of the msi package?

    Regarding the script, use the console (Plugins->Python Script->Show console) to get the errors and help.
    If you type help(name_of_the_function) like help(editor.gotoPos) into the >>> inputbox,
    you will get additional informations about that function and you can use it to test a particular function.
    Btw. python is case sensitive.

    Cheers
    Claudia



  • Thanks all. It’s not very elegant but I got it working. :)

    field = “foo =”
    initial_value = 1
    delta_value = 0.05
    ignore_records = 3
    record_n = 0

    editor.gotoLine(0)
    editor.searchAnchor()

    while editor.searchNext(0,field) != -1 :
    editor.gotoPos( editor.searchNext(0,field) )
    record_n += 1
    if record_n > ignore_records :
    initial_value += delta_value
    editor.setSel( editor.getAnchor() + len(field), editor.getLineEndPosition(editor.lineFromPosition(editor.getCurrentPos())) )
    editor.replaceSel( str(initial_value) + ‘f’ )
    editor.searchAnchor()



  • That’s what it is all about - get it working.
    One thing which makes me wonder is that record_n doesn’t get reset,
    so after 3rd run

    if record_n > ignore_records :
    

    is always true.

    Btw. if you post code, intend the whole code by 4 spaces then it keeps the layout ;-)

    Cheers
    Claudia


Log in to reply