Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
replace line
#1
i know getl will get a certian line number but is there something that will "replace" line x with string y?

thanks.
#2
Create new member function str.ReplaceLineN and paste this code:

Code:
Copy      Help
function# $replacement lineindex

;Replaces specified line.
;Returns index of first character of n-th line, or -1 if n is too big.


;replacement - replacement string.
;lineindex - zero-based line index.


;EXAMPLE
;str s="zero[]one[]two"
;s.ReplaceLineN("ONE" 1)
;out s



int i=findl(this lineindex)
if(i>=0)
,int j=findcs(this "[]" i)
,this.replace(replacement i iif(j>=0 j-i -1))
ret i
#3
Thanks.

however, when i try to name it "str.ReplaceLineN" it replaces the "." with "_". if i try to name it as ReplaceLineN, the macro doesnt recognize it. i changed str.ReplaceLineN to be a function; is there something else i need to do?
thanks.
#4
Menu File -> New -> New Member Function.
#5
SWEEEEEEET!!!!

works great!!!!

thanks.... Big Grin Big Grin
#6
two further thoughts on this.
  • is there a way to delete the entire line?
    is there a way to insert line(s)?
An old blog on QM coding and automation.

The Macro Hook
#7
Member function str.AddLine
Code:
Copy      Help
function $s

;Appends s as new line.

;EXAMPLE
;str sf
;sf.AddLine("line1")
;sf.AddLine("line2")
;...



if(this.len and !this.end("[]")) this+"[]"
this+s
this+"[]"

Member function str.ReplaceLineN
Code:
Copy      Help
function# $replacement lineindex [nlines]

;Replaces specified line(s).
;Returns index of first character of lineindex-th line, or -1 if lineindex is too big.


;replacement - replacement string.
;lineindex - zero-based line index.
;nlines - number of lines to replace. Default or 0: 1 line.


;EXAMPLE
;str s="zero[]one[]two"
;s.ReplaceLineN("ONE" 1)
;out s



if(nlines<1) nlines=1
int i=findl(this lineindex)
if(i>=0)
,int j=findl(this+i nlines)
,if(j>=0) j-2; if(j<i or this[i+j]!=13) j+1
,this.replace(replacement i j)
ret i

Member function str.RemoveLineN
Code:
Copy      Help
function# lineindex [nlines]

;Removes specified line(s).
;Returns index of first character of lineindex-th line, or -1 if lineindex is too big.


;lineindex - zero-based line index.
;nlines - number of lines to remove. Default or 0: 1 line.


;EXAMPLE
;str s="zero[]one[]two"
;s.RemoveLineN(1)
;out s



if(nlines<1) nlines=1
int i=findl(this lineindex)
if(i>=0) this.remove(i findl(this+i nlines))
ret i

Member function str.InsertLineN
Code:
Copy      Help
function# $insertstring lineindex

;Inserts line.
;Returns index of first character of lineindex-th line, or -1 if lineindex is too big.


;insertstring - string to be inserted. Does not have to end with new line.
;lineindex - zero-based line index where to insert insertstring.


;EXAMPLE
;str s="zero[]one[]two"
;s.InsertLineN("" 1) ;;inserts empty line
;out s



int i=findl(this lineindex)
if(i<0 and numlines(this)=lineindex) this+"[]"; i=this.len
if(i>=0)
,str s.from(insertstring "[]")
,this.insert(s i s.len)
ret i
#8
NIIIIIICccccee!!!

thanks.
An old blog on QM coding and automation.

The Macro Hook


Forum Jump:


Users browsing this thread: 1 Guest(s)