Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Arrays reverse order, splitting one line of strings
#1
Hello everyone,

I want to achieve the same effect in QM as powershell code, 
Included: Arrays reverse order, and splitting one line of strings

Code Description:
Split the last line of string from where the first # character, 
All Lines, in reverse order, then merged together with spaces between, Final after the #character is placed at the end(keep #)

The end result:
I'm in the third line I'm the second line I'm in the first line #foo #ball

Powershell execution is slow, using QM should be faster, 

Thanks for any advice and help
david


Macro Macro7
Code:
Copy      Help
out
_s = 
;I'm in the first line
;I'm the second line
;I'm in the third line #foo #ball

;todo: split The third line of the string, separator # keep #

ARRAY(str) a
a=_s
a.sort(1) ;;Reverse order does not take effect
str d

for _i 0 a.len
,d.formata("%s " a[_i].trim)
out d


Powershell code:
Code:
Copy      Help
 
$s = @'
I'm in the first line
I'm the second line
I'm in the third line #foo #ball
'@

$arr = $s -split '\n'
$x, $y = $arr[-1] -split '(?=#)', 2
$arr = @(
  if ($null -ne $y) { $y }
  $arr | Select-Object -SkipLast 1
  $x.trim()
)
[array]::Reverse($arr)
$arr -join ' '
#2
Macro Macro3208
Code:
Copy      Help
out
_s =
;I'm in the first line
;I'm the second line
;I'm in the third line #foo #ball

ARRAY(str) a=_s

;get and remove the #... part of the last line
str tail
str& last=a[a.len-1]
int j=findcs(last "?=#"); if(j>=0) tail.get(last j); last.left(last j); last.trim

;reverse
for(int'i 0 a.len/2) a[i].swap(a[a.len-i-1])
if(j>=0) a[]=tail

;join
str d=a[0]
for _i 1 a.len
,d.formata(" %s" a[_i])
out d
#3
@Gintaras 
Thanks for your help

I have trouble understanding the code below,    use  a.sort  be implemented using parameters?

;reverse
for(int'i 0 a.len/2) a.swap(a[a.len-i-1])
-------------------------------------------------------------------------------------------------

I found a function that splits the Line string, but didn't keep #

Macro Macro9
Code:
Copy      Help
_s = "I'm in the third line #foo #ball"
ARRAY(str) a
int i=tok(_s a 2 "?=#" 2)
out i
_s=a
out _s


Forum Jump:


Users browsing this thread: 1 Guest(s)