Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie. Split string. Replace.
#1
Hello everybody!

Friend of mine pointed me to QM and i'm trying to learn it and what i've seen so far is very nice!

I've a problem and maybe some of you can help me with this.. i'm trying to make a macro which has as input a line like this:

hello|goodbey|welcome|light|dark

what i want with the input-line is to split it up in seperate strings for further usage and the "|" is the mark to split. Can that be done? and how?

Any help is welcome!
#2
Macro Macro1476
Code:
Copy      Help
;input string
str s="hello|goodbey|welcome|light|dark"

;split
ARRAY(str) a
tok s a -1 "|"

;results
int i
for i 0 a.len
,out a[i]
#3
Thank you so much!

Is it also possible to replace the second word (goodbey) with something else for example 'replaced' ? even if there is for example a line with
"hello||welcome|light|dark" ?

so the part with the 'results' has to look for the second splitted result and replace this with 'replaced' ...

hope you can help me with this also would be amazing!
#4
Maybe better to use CSV format. Because tok would ignore empty words.

Macro Macro1431
Code:
Copy      Help
;input string (2 lines)
str s=
;hello|goodbey|welcome|light|dark
;hello||welcome|light|dark

;split
ICsv x=CreateCsv
x.Separator="|"
x.FromString(s)

;replace second word in each line
int i
for i 0 x.RowCount
,x.Cell(i 1)="replaced"

;results
for i 0 x.RowCount
,out "%s, %s, %s, %s, %s" x.Cell(i 0) x.Cell(i 1) x.Cell(i 2) x.Cell(i 3) x.Cell(i 4)
#5
thanks!

One last question if i can...

How to put the output in a textfile?
#6
x.ToFile("$desktop$\split replace.txt")
#7
Thanks


Forum Jump:


Users browsing this thread: 1 Guest(s)