RegEx (again)
Moderators: JRL, Dorian (MJT support)
RegEx (again)
Hello Everyone,
I searched the web and played around. I tried to figure this out, but with no luck.
I have a string....
let>MyString=3714
The numbers in MyString will always be between 1 and 7. No delimeters, but each digit is a separate number. And MyString will never be more than 4 digits long.
I need a RegEx pattern that will sort these numbers numerically.
So, in this case, MyString would equal 1347, after sorting.
Will someone please help me out?
Thanks in advance!
PepsiHog
I searched the web and played around. I tried to figure this out, but with no luck.
I have a string....
let>MyString=3714
The numbers in MyString will always be between 1 and 7. No delimeters, but each digit is a separate number. And MyString will never be more than 4 digits long.
I need a RegEx pattern that will sort these numbers numerically.
So, in this case, MyString would equal 1347, after sorting.
Will someone please help me out?
Thanks in advance!
PepsiHog
Windows 7
PepsiHog. Yep! I drink LOTS of Pepsi (still..in 2024) AND enjoy programming. (That's my little piece of heaven!)
The immensity of the scope of possibilities within Macro Scheduler pushes the user beyond just macros!
PepsiHog. Yep! I drink LOTS of Pepsi (still..in 2024) AND enjoy programming. (That's my little piece of heaven!)
The immensity of the scope of possibilities within Macro Scheduler pushes the user beyond just macros!
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
RegEx can't do the sorting itself, but we can use it to split the string into an array of matches (matches being an individual number) and then we can sort that array and then concatenate back to a string:
Code: Select all
let>MyString=3714
RegEx>\d?,MyString,0,matches,nm,0
ArraySort>matches
Let>MyString=
Let>x=0
Repeat>x
Let>x=x+1
Let>this=matches_%x%
Let>MyString=%MyString%%this%
Until>x=nm
MessageModal>MyString
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
thanks
Thanks Marcus!
However, I had already written something similar. Funny, same number of lines as yours.
I'm writting a long macro, so whenever possible I try to go with the shortest solution.
If you are curious, this was what I came up.
Thanks for helping anyway.
PepsiHog
However, I had already written something similar. Funny, same number of lines as yours.
I'm writting a long macro, so whenever possible I try to go with the shortest solution.
If you are curious, this was what I came up.
Code: Select all
let>MyString=3175
let>asd=0
RegEx>\d,MyString,0,matches,nom,0
Repeat>asd
let>asd=%asd%+1
let>Temp=matches_%asd%
let>String_%Temp%=%Temp%
let>Sort=%String_1%%String_2%%String_3%%String_4%%String_5%%String_6%%String_7%
RegEx>%String_\d%,Sort,0,res,mn,1,,Sort
Until>asd=%nom%
mdl>%Sort%
PepsiHog
Windows 7
PepsiHog. Yep! I drink LOTS of Pepsi (still..in 2024) AND enjoy programming. (That's my little piece of heaven!)
The immensity of the scope of possibilities within Macro Scheduler pushes the user beyond just macros!
PepsiHog. Yep! I drink LOTS of Pepsi (still..in 2024) AND enjoy programming. (That's my little piece of heaven!)
The immensity of the scope of possibilities within Macro Scheduler pushes the user beyond just macros!
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Well if the string is always 4 chars long you could just do:
let>MyString=3714
RegEx>\d?,MyString,0,matches,nm,0
ArraySort>matches
Let>MyString=%matches_1%%matches_2%%matches_3%%matches_4%
It might make more sense to write a subroutine which sorts a string of numbers. Then you can call it with a one line GoSub when needed. I'd choose maintainability, readability and reuse over code size. We're not writing an algorithm to squeeze into a 1980s calculator.
let>MyString=3714
RegEx>\d?,MyString,0,matches,nm,0
ArraySort>matches
Let>MyString=%matches_1%%matches_2%%matches_3%%matches_4%
It might make more sense to write a subroutine which sorts a string of numbers. Then you can call it with a one line GoSub when needed. I'd choose maintainability, readability and reuse over code size. We're not writing an algorithm to squeeze into a 1980s calculator.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
So, for e.g., here's a reusable subroutine to sort any string, with an example of use:
Code: Select all
Let>MyString=317536492750930
GoSub>CharSort,MyString,{"MyString"}
MessageModal>MyString
// *** subroutines *** //
//CharSort, parm1: the string to sort, parm2: name of variable to return
SRT>CharSort
Let>LOCALVARS=1
Let>tmp=
RegEx>.,CharSort_Var_1,0,matches,nm,0
ArraySort>matches
Let>x=0
Repeat>x
Let>x=x+1
Let>this_char=matches_%x%
Let>tmp=%tmp%%this_char%
Until>x=nm
Let>LOCALVARS=0
Let>%CharSort_Var_2%=tmp
END>CharSort
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
thanks again
Great code, Marcus!
Thanks a lot! That's EVEN shorter! ArraySort never managed to get into my common use list, but it is now.
The macro I'm writting is really just for fun and it's a pretty big challenge. I won't say what it does. (I don't want one of these smarties to beat me to the punch.)
But the reason I'm concerned with length is because it should be really cool and I would like to share it here on the forum when it's done.
But if it's too long, I can't do that.
Have any thoughts on sharing a lengthy macro with forum users?
I wouldn't mind having a "plan B".
PepsiHog
Thanks a lot! That's EVEN shorter! ArraySort never managed to get into my common use list, but it is now.
The macro I'm writting is really just for fun and it's a pretty big challenge. I won't say what it does. (I don't want one of these smarties to beat me to the punch.)
But the reason I'm concerned with length is because it should be really cool and I would like to share it here on the forum when it's done.
But if it's too long, I can't do that.
Have any thoughts on sharing a lengthy macro with forum users?
I wouldn't mind having a "plan B".

PepsiHog
Windows 7
PepsiHog. Yep! I drink LOTS of Pepsi (still..in 2024) AND enjoy programming. (That's my little piece of heaven!)
The immensity of the scope of possibilities within Macro Scheduler pushes the user beyond just macros!
PepsiHog. Yep! I drink LOTS of Pepsi (still..in 2024) AND enjoy programming. (That's my little piece of heaven!)
The immensity of the scope of possibilities within Macro Scheduler pushes the user beyond just macros!
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
If you want to share it and if short means it becomes less readable and understandable then I would go with long. A good way to share it would be to use dropbox or similar. Save the .scp file in your public folder, get the link and paste the link into the forum. People can then download it as a raw .scp file.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
WOW!
WOW! I did not know about dropbox. That is awesome! So much for worrying about the length of my macro.
Great idea, Marcus!
I can't thank you enough! And I have no doubt this will help others as well.
Thank ya, thank ya, thank ya.
PepsiHog
Great idea, Marcus!
I can't thank you enough! And I have no doubt this will help others as well.
Thank ya, thank ya, thank ya.

PepsiHog
Windows 7
PepsiHog. Yep! I drink LOTS of Pepsi (still..in 2024) AND enjoy programming. (That's my little piece of heaven!)
The immensity of the scope of possibilities within Macro Scheduler pushes the user beyond just macros!
PepsiHog. Yep! I drink LOTS of Pepsi (still..in 2024) AND enjoy programming. (That's my little piece of heaven!)
The immensity of the scope of possibilities within Macro Scheduler pushes the user beyond just macros!
And with a few alterations to the repeat loop you can have a reverse sort. Not that that was requested but it seems this is still the place to mention it.Marcus wrote:So, for e.g., here's a reusable subroutine to sort any string, with an example of use:
Code: Select all
Let>MyString=317536492750930
GoSub>ReverseCharSort,MyString,{"MyString"}
MessageModal>MyString
// *** subroutines *** //
//ReverseCharSort, parm1: the string to sort, parm2: name of variable to return
SRT>ReverseCharSort
Let>LOCALVARS=1
Let>tmp=
RegEx>.,ReverseCharSort_Var_1,0,matches,nm,0
ArraySort>matches
Let>x={%nm%+1}
Repeat>x
Let>x=x-1
Let>this_char=matches_%x%
Let>tmp=%tmp%%this_char%
Until>x=1
Let>LOCALVARS=0
Let>%ReverseCharSort_Var_2%=tmp
END>ReverseCharSort