Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find & Remove String From List
#1
Good morning QM Forum,

I've been trying to accomplish this task without asking, but I'm stuck at the last bit of information that I need. I'm working on a collaborative macro that displays online users in the main dialog box. It gets this information from a web page using InitGetFile. I've simulated that process with the accountList str. When a user signs out of the software I want to remove them from the list. If the account isn't the first in the list the example works perfectly, but if the account is the first item in the list it will leave the top spot empty instead of replacing it.

I know that I can find the line of the username with numlines and can check if line = 0, but I'm not sure how to remove the empty item at the top.

Macro Example String Replacement
Code:
Copy      Help
;; simulates the info received from webpage
str accountList = "account1[]account2[]account3[]account4[]account5"
str getAccount  = "account1"

accountList.replacerx(getAccount "")
accountList.findreplace("[][]" "[]" 8)
out accountList
#2
Currently working on implementing the str.RemoveLineN member function listed here: http://www.quickmacros.com/forum/showthr...mpty+lines

I was able to figure out how to remove the space if it's the first item, but now working out the kinks to remove the space if it's the last item.
#3
this should work for you

Macro [b]Example String Replacement[/b]
Code:
Copy      Help
;;;simulates the info received from webpage
str accountList = "account1[]account2[]account3[]account4[]account5"
str getAccount  = "account1"
getAccount +"[]"
accountList.replacerx(getAccount "" 8)
out accountList
#4
Thank you Kevin! This works for my initial problem and the follow up post.
#5
after looking further this is a better way to go converts to array finds item and removes it then converts back to multiline string

Macro Example String Replacement
Code:
Copy      Help
;;;simulates the info received from webpage
str accountList = "account1[]account2[]account3[]account4[]account5"
str getAccount  = "account5"

ARRAY(str) arr
int i nt
nt = tok(accountList arr)
for(i 0 nt)
,if arr[i]= getAccount
,,int rs=i
arr.remove(rs)    
accountList=arr
accountList.trim
out accountList
#6
I've incorporated your other post and everything works great. An array will definitely help once the user list gets larger. I will definitely incorporate this into everything. Thank you sir!
#7
I apologize when i posted 1st time i was half asleep and the initial post wont work for all circumstances

if the user  is the last line of the string this won't work.
Macro Example String Replacement
Code:
Copy      Help
;;;simulates the info received from webpage
str accountList = "account1[]account2[]account3[]account4[]account5"
str getAccount  = "account5"
getAccount +"[]"
accountList.replacerx(getAccount "" 8)
out accountList

so use either this

Macro Example String Replacement1
Code:
Copy      Help
;;;simulates the info received from webpage
str accountList = "account1[]account2[]account3[]account4[]account5"
str getAccount  = "account5"
ARRAY(str) arr
int i nt
nt = tok(accountList arr)
for(i 0 nt)
,if arr[i]= getAccount
,,int rs=i
arr.remove(rs)    
accountList=arr
accountList.trim
out accountList

or use this
Macro Example String Replacement2
Code:
Copy      Help
;;;simulates the info received from webpage
str accountList = "account1[]account2[]account3[]account4[]account5"
str getAccount  = "account5"
str s
int x
foreach s accountList
,if getAccount = s
,,accountList.RemoveLineN(x)
,x+1
accountList.trim
out accountList

Example String Replacement2 needs the function below

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
#8
I'm glad you pointed that out because I did not notice it at first since the user I was testing was never in the situation of being last unless if was the only account. In that situation it defaults to "None" upon close. I will add these updates. Thank you for the dual examples!


Forum Jump:


Users browsing this thread: 1 Guest(s)