Finding postiton of NOT-specific character in a string?

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
rullbandspelare
Pro Scripter
Posts: 149
Joined: Tue Mar 23, 2004 9:11 pm

Finding postiton of NOT-specific character in a string?

Post by rullbandspelare » Sun Nov 15, 2009 1:12 pm

Hi!
I just whanted to hear if someone smart has any comment on this.

I have a 2.000.000 characters long string that has to be manipulated fast. Most of the characters are 0 (space and zero) but eventualy another number is present.

An obvious method would be to check every character with "Position>" but I whant the FASTEST way to find the occurence of a non zero number.

Is there a smarter way that I have not thought about. With RegEx and such?

The string looks like
0 0 0 0 0 0 0 0 0 0 0 0%lf% 0 0 0 0 0 0 0 0 0 0 0 0%lf% 0 0 0 0 0 0 0 0 34 34 34 0 0 0%lf% 0 0 0 0 0 0 0 0 0 0 0 0%lf%

I Can also get the string in this format
1,1: (0, 0, 0,)%CRLF%1,2: (0, 0, 0,)%CRLF%1,3: (34, 34, 34,)%CRLF%1,4: (0, 0, 0,)%CRLF%1,5: (0, 0, 0,)

Thanks!

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Mon Nov 16, 2009 12:05 am

Use RegEx, looking for anything not a zero .

That will give you a count of those characters, and will assign a variable to each one of them so that you can do more manipulation. You could also replace each of those characters with another character or string, etc.

Here is an untested example:

Code: Select all

Let>vNeedle=%space%[1-9]+[0-9]*%space%
Let>vHaystack=0 0 0 0 0 0 0 0 0 0 0 0%lf% 0 0 0 0 0 0 0 0 0 0 0 0%lf% 0 0 0 0 0 0 0 0 34 34 34 0 0 0%lf% 0 0 0 0 0 0 0 0 0 0 0 0%lf%
Let>vReplace=%space%xxx%space%
RegEx>%vNeedle%,%vHaystack%,0,vMatch,vMatchCount,1,%vReplace%,vNewString
MessageModal>New String is %vNewString%
This may need some refining.

1. Assume we are ignoring the %lf%, correct?

2. The definition of "the occurence of a non zero number" needs more clarity. How many digits could it be?

3. Would there be any leading zeros? For example, if two digits, would seven be "07" or "7" ?

4. What is the range of acceptable numbers?

EDITED NOTE: The sample code above is superceded by my next posting that has a TESTED script that works better.
Last edited by Bob Hansen on Mon Nov 16, 2009 8:09 pm, edited 1 time in total.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Mon Nov 16, 2009 7:34 pm

Here is a tested version that works better:

Code: Select all

Let>vNeedle=[1-9]+[0-9]*%space%
Let>vHaystack=0 0 0 0 0 0 0 0 0 0 0 0%lf% 0 0 0 0 0 0 0 0 0 0 0 0%lf% 0 0 0 0 0 0 0 0 34 34 34 0 0 0%lf% 0 0 0 0 0 0 0 0 0 0 0 0%lf%
Let>vReplace=xxx%space%
RegEx>%vNeedle%,%vHaystack%,0,vMatch,vMatchCount,1,%vReplace%,vNewString
MessageModal>New String is %vNewString%
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Mon Nov 16, 2009 11:12 pm

And here is a better expression that will handle any number of digits that is not 0, with or without leading zeros:

Code: Select all

Let>vNeedle=\b[0-9]*[1-9][0-9]*\b
Let>vHaystack=0 0 0 0 0 0 0 0 0 0 0 0%lf% 0 0 0 0 0 0 0 0 0 0 0 0%lf% 0 0 0 0 0 0 0 0 34 34 34 0 0 0%lf% 0 0 0 0 0 0 0 0 0 0 0 0%lf%
Let>vReplace=xxx
RegEx>%vNeedle%,%vHaystack%,0,vMatch,vMatchCount,1,%vReplace%,vNewString
MessageModal>New String is %vNewString%
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

rullbandspelare
Pro Scripter
Posts: 149
Joined: Tue Mar 23, 2004 9:11 pm

Post by rullbandspelare » Tue Nov 17, 2009 9:48 pm

Thanks for Your help.
And thanks for the RegEx examples.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts