Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
equivalent QM code for loops through in a PowerShell pipeline
#1
Hi,

I want to change Multi-line string A to another format B, I have the correct PowerShell code,
This key and value are separated by -  Key-value pairs are separated by ---

but I want to find the equivalent QM code (loops through in a PowerShell pipeline), I tried many times but it didn't work

Thanks in advance for any advice and help
david

______________________________A______________________________before

A Key
-
Hello 1
World 1
---
B Key
-
Hello 2

World 2
---
C Key
-
Hello 3
World 3
_____________________________B_______________________________after
str A=
 A Key
str Av=
 Hello 1
 World 1
Av.findreplace("[]" "┋")

str B=
 B Key
str Bv=
 Hello 2
 
 World 2
Bv.findreplace("[]" "┋")

str C=
 C Key
str Cv=
 Hello 3
 World 3
Cv.findreplace("[]" "┋")

str kv=
F
 {A}={Av}
 {B}={Bv}
 {C}={Cv}
DynamicLV(kv)

Powershell Code: 
 
Code:
Copy      Help
cls
$s = @'
A Key
-
Hello 1
World 1
---
B Key
-
Hello 2

World 2
---
C Key
-
Hello 3
World 3
'@

$ci = 65
$s -split "\r\n---\r\n" | %{
    $b = $_ -split "\r\n-\r\n"
    $c = [char]$ci
    "str $c=`r`n $($b[0])"
    "str ${c}v="
    $b[1] -split "\r\n" | %{ " $_" }
    "${c}v.findreplace(`"[]`" `"┋`")`r`n"
    $ci++
}
'str kv='
'F'
65 .. ($ci - 1) | %{ " {$([char]$_)}={$([char]$_)v}" }
'DynamicLV(kv)'


Macro Macro1
Code:
Copy      Help
out
_s=
;A Key
;-
;Hello 1
;World 1
;---
;B Key
;-
;Hello 2
;
;World 2
;---
;C Key
;-
;Hello 3
;World 3

ARRAY(str) a
tok(_s a -1 "---" 0x2000)
str as=a

str d
int c=65

foreach str'as_ as
,ARRAY(str) b
,tok(as_ b -1 "-" 0x2000)
,d.formata("str %c=[] %s[]str %cv=" c b[0] c)
,ARRAY(str) cc
,tok(b[1] cc -1)
,str ccs=cc
,foreach str'ccs_ ccs
,,d.formata("[]%c.findreplace(''[]'' ''┋'')[]" c b[0] c)
,c+1
;out d
for _i 65 c-1
,d.formata("str kv=[]F[] {%c}={%cv}[]DynamicLV(kv)" _i _i)
;out d
#2
succeed

Macro Macro3
Code:
Copy      Help
out
_s=
;A Key
;-
;Hello 1
;World 1
;---
;B Key
;-
;Hello 2
;
;World 2
;---
;C Key
;-
;Hello 3
;World 3

_s.replacerx("(?m)^-$" "+")
ARRAY(str) a
tok(_s a -1 "-" 0x2000)

str d
int c=65

for _i 0 a.len
,ARRAY(str) b
,tok(a[_i] b -1 "+" 0x2000)
,d.formata("str %c=[] %s[]str %cv=" c b[0] c)
,foreach _s b[1]
,,d.formata("[] %s" _s)
,d.formata("[]%cv.findreplace(''[91]]'' ''┋'')[][]" c)
,c+1

d.formata("str kv=[]F[]")

for _i 65 c
,d.formata(" {%c}={%cv}[]" _i _i)

d.formata("DynamicLV(kv)")
out d

There may be a better solution, if anyone knows, please let me know, thanks


Forum Jump:


Users browsing this thread: 1 Guest(s)