margaret Posted January 27, 2010 Report Share Posted January 27, 2010 How would I go about counting the number of spaces in a string? The string will already have been trimmed on both sides, and will already have had any instances of two spaces in a row changed to one space. What I want to do is to evaluate the string, and if it only contains two words (hence one space), stop the macro, because that macro is designed to deal with a string of more than two words. I know how to find out if the string doesn't contain any spaces, but how would I find out if there's only one? Thanks! Quote Link to comment Share on other sites More sharing options...
rberq Posted January 27, 2010 Report Share Posted January 27, 2010 Move your string to another text variable (T99, say) so the original won't be destroyed by the following. Then VARIABLE SET INTEGER N99 from position of text (space) in T99. If N99 is zero there are no spaces at all in T99, so you are done. If N99 is greater than zero, add 1 to counter. VARIABLE MODIFY STRING replace substring " " with "x" in T99 (choose the option to replace just one instance, not all instances). At this point you have counted the first space (if there is one), and then replaced it with the letter "x". Now repeat the above until N99 is zero (all spaces have been counted and replaced by "x"). Whatever is in your counter is the number of blanks that were in the original string. Quote Link to comment Share on other sites More sharing options...
paul Posted January 28, 2010 Report Share Posted January 28, 2010 Suppose your string is stored in T1. Save the length of T1 into N1. Replace, into T2, all spaces in T1 with nothing (""). Save the length of T2 into N2. Subtract, into N3, N2 from N1. N3 now contains the number of spaces in T1, whether 0, 1 or more. FYI, this works much faster with long strings in ME3 than in ME4 Pro! Quote Link to comment Share on other sites More sharing options...
rberq Posted January 28, 2010 Report Share Posted January 28, 2010 You win, Paul. I like yours better. Quote Link to comment Share on other sites More sharing options...
paul Posted January 28, 2010 Report Share Posted January 28, 2010 You win, Paul. I like yours better. Where's my prize? Quote Link to comment Share on other sites More sharing options...
kevin Posted January 28, 2010 Report Share Posted January 28, 2010 FYI, this works much faster with long strings in ME3 than in ME4 Pro! Is this still true after the variable commands were optimized in Macro Express Pro? Have you tried it with Macro Express Pro v 4.1.3.1 or later? Quote Link to comment Share on other sites More sharing options...
margaret Posted January 28, 2010 Author Report Share Posted January 28, 2010 Suppose your string is stored in T1. Save the length of T1 into N1. Replace, into T2, all spaces in T1 with nothing (""). Save the length of T2 into N2. Subtract, into N3, N2 from N1. N3 now contains the number of spaces in T1, whether 0, 1 or more. This is great, Paul. Thanks! rberq' s solution works as well, but took me longer to write up. Naturally, I had it figured out his way before I saw yours... Meg Quote Link to comment Share on other sites More sharing options...
paul Posted January 28, 2010 Report Share Posted January 28, 2010 Is this still true after the variable commands were optimized in Macro Express Pro? Have you tried it with Macro Express Pro v 4.1.3.1 or later? Definitely! I wrote the following in MEP and in AutoIt. - Read a text file into t1 (length of file is 11.7 MB) - Save length of t1 into n1 - Replace all spaces in t1 with nothing - Save length of t1 into n2 - Subtract n2 from n1 into n3 - Display n1, n2, and n3 In MEP, I aborted the macro after 5 minutes! Lines 1 and 2 executed instantly, Line 3 never completed. In AutoIt, the entire script completed in 2 seconds and returned the correct answers. Quote Link to comment Share on other sites More sharing options...
rberq Posted January 28, 2010 Report Share Posted January 28, 2010 Where's my prize? The check is in the mail. Honest .... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.