Jump to content
Macro Express Forums

Optimize a string reversal


Recommended Posts

For a script I am developing, I need to reverse the order of characters. For example, "12345"  needs to transform into "54321". Macro Express does not appear to have an instruction for reversing strings, so I wrote a script that does just that.

 

I would be curious to know if there are ways to make a script like this run faster. It's quick for strings consisting of a few dozen or hundred characters, but noticeably slower when the string contains thousands of characters.

 

(This test script is set to use "0123456789" as its input.)

 

// String reverse
//      %Text% = String before processing
//      %Result% = String after processing
Variable Set String %Text% to "0123456789"
Variable Set String %Result% to ""
 
//      %Len% = Number of characters in %Text%
//      %Start% = Starting position of the character to copy. (Begin with the last one)
Variable Set Integer %Len% to the length of variable %Text%
Variable Set Integer %Start% to %Len%
 
// Begin by copying the final character in %Text%. Next time, copy the second last character, etc.
// Each time, append the character, %Char%, to the end of %Result%
Repeat Start (Repeat %Len% times)
  Variable Modify String: Copy part of text in %Text% starting at %Start% and 1 characters long to %Char%
  Variable Modify String %Result%: Append Text (%Char%)
  Variable Modify Integer %Start%: Decrement
End Repeat
 
Text Box Display: Result
<COMMENT Value="String reverse"/>
<COMMENT Value="     %Text% = String before processing"/>
<COMMENT Value="     %Result% = String after processing"/>
<VARIABLE SET STRING Option="\x00" Destination="%Text%" Value="0123456789" NoEmbeddedVars="FALSE"/>
<VARIABLE SET STRING Option="\x00" Destination="%Result%" NoEmbeddedVars="FALSE"/>
<COMMENT/>
<COMMENT Value="     %Len% = Number of characters in %Text%"/>
<COMMENT Value="     %Start% = Starting position of the character to copy. (Begin with the last one)"/>
<VARIABLE SET INTEGER Option="\x0D" Destination="%Len%" Text_Variable="%Text%"/>
<VARIABLE SET INTEGER Option="\x00" Destination="%Start%" Value="%Len%"/>
<COMMENT/>
<COMMENT Value="Begin by copying the final character in %Text%. Next time, copy the second last character, etc."/>
<COMMENT Value="Each time, append the character, %Char%, to the end of %Result%"/>
<REPEAT START Start="1" Step="1" Count="%Len%" Save="FALSE"/>
<VARIABLE MODIFY STRING Option="\x09" Destination="%Char%" Variable="%Text%" Start="%Start%" Count="1" NoEmbeddedVars="FALSE"/>
<VARIABLE MODIFY STRING Option="\x06" Destination="%Result%" Value="%Char%" NoEmbeddedVars="FALSE"/>
<VARIABLE MODIFY INTEGER Option="\x08" Destination="%Start%"/>
<END REPEAT/>
<COMMENT/>
<TEXT BOX DISPLAY Title="Result" Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Tahoma;}{\\f1\\fnil Tahoma;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs40 %Result%\\f1 \r\n\\par }\r\n" Left="Center" Top="Center" Width="278" Height="200" Monitor="0" OnTop="TRUE" Keep_Focus="TRUE" Mode="\x00" Delay="0"/>
<END REPEAT _ENABLED="FALSE"/>
<TEXT BOX DISPLAY Title="Done" Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil Tahoma;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs20 \r\n\\par }\r\n" Left="Center" Top="Center" Width="278" Height="200" Monitor="0" OnTop="TRUE" Keep_Focus="TRUE" Mode="\x00" Delay="0" _ENABLED="FALSE"/>

 

Link to comment
Share on other sites

 

Hi Alan,

 

Saw this just before dashing out so have not yet tried your script. I expect this old one of mine will be much slower, designed for relatively short strings. Note that the Variable Restore is disabled. This was intended as a submacro.

 

Later...

 

Terry

 

EDIT: Back home and tested both. Indistinguishable difference in performance until I got to 5,000 random characters when mine seemed very slightly faster.

 

 

Variable Restore: Restore Text Variables // Restore Input from a previous macro
// Disable the next TBD if you want silent running.
Variable Set String %Input%: Prompt
Variable Set Integer %Length% to the length of variable %Input%
Repeat Start (Repeat %Length% times)
  Variable Modify String: Copy part of text in %Input% starting at %Index% and 1 characters long to %Char%
  Variable Modify String %Output%: Append Text String Variable (%Char%)
End Repeat
// Disable the next TBD if you want silent running.
Text Box Display: Full Input string reversed


<VARIABLE RESTORE Option="\x01" _ENABLED="FALSE" _COMMENT="Restore Input from a previous macro"/>
<COMMENT Value="Disable the next TBD if you want silent running."/>
<VARIABLE SET STRING Option="\x01" Destination="%Input%" Prompt="Please input a string of characters" Mask="FALSE" OnTop="FALSE" Left="Center" Top="Center" Monitor="0"/>
<VARIABLE SET INTEGER Option="\x0D" Destination="%Length%" Text_Variable="%Input%"/>
<REPEAT START Start="%Length%" Step="-1" Count="%Length%" Save="TRUE" Variable="%Index%"/>
<VARIABLE MODIFY STRING Option="\x09" Destination="%Char%" Variable="%Input%" Start="%Index%" Count="1" NoEmbeddedVars="FALSE"/>
<VARIABLE MODIFY STRING Option="\x07" Destination="%Output%" Variable="%Char%" NoEmbeddedVars="FALSE"/>
<END REPEAT/>
<COMMENT Value="Disable the next TBD if you want silent running."/>
<TEXT BOX DISPLAY Title="Full Input string reversed" Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Tahoma;}{\\f1\\fnil Tahoma;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs16 %Output%\\f1 \r\n\\par }\r\n" Left="Center" Top="Center" Width="278" Height="200" Monitor="0" OnTop="TRUE" Keep_Focus="TRUE" Mode="\x00" Delay="0"/>

 

Link to comment
Share on other sites

Terry,

 

Your solution is cleaner and more elegant than mine, and I can imagine it might run a bit faster. Thank you for posting!

 

It's interesting, educational, and entertaining to develop one's own string manipulation tools. I was going to write Insight and request they add features like reverse, pre-pend to a string (to complement the already-existing append). But if Insight had added these features in the past, I wouldn't have taken the time to learn how to build them myself!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...