f



Detect strings of repeated characters

Hi,

I'm trying to find out how to detect repeated characters (such as "aaaaaa", "gggggg", "ZZZ", ...)

Anyone has got an idea how to do it?

Thanks!
Cedric
0
cedalexandre82
9/21/2016 2:51:37 PM
comp.soft-sys.stat.spss 5679 articles. 0 followers. Post Follow

2 Replies
208 Views

Similar Articles

[PageSpeed] 19

On Wed, 21 Sep 2016 07:51:37 -0700 (PDT), cedalexandre82@gmail.com
wrote:

>Hi,
>
>I'm trying to find out how to detect repeated characters (such as "aaaaaa", "gggggg", "ZZZ", ...)
>
>Anyone has got an idea how to do it?
>

Obviously, you have to have a loop in which you pick up each
character and compare it to something - first character or 
previous character, it seems - until you reach the end of
the non-blank part of the string.  Consider LTRIM?  RTRIM? 

If you only want to know that some character is repeated,
somewhere, you compare each to the previous, and quit
once there is a single match. 

What you do in detail depends on what you want to know and
report. 

-- 
Rich Ulrich 


0
Rich
9/21/2016 6:05:36 PM
Here is the simplest solution I could muster:

DATA LIST FREE / str (a10).
BEGIN DATA
ssssssffdf
wdddwwwwdd
aaaaaaaaaa
iiiieiiiei
END DATA.

STRING #Prev (A1).
COMPUTE #Prev=CHAR.SUBSTR(str,1,1).
COMPUTE #cur=2.

LOOP.
+  DO IF CHAR.SUBSTR(str,#cur,1) EQ #Prev.
+    COMPUTE str=CONCAT(CHAR.SUBSTR(str,1,#cur-1),CHAR.SUBSTR(str,#cur+1) ).
+  ELSE.
+    COMPUTE #cur=#cur+1.
+  END IF.
+  COMPUTE #Prev=CHAR.SUBSTR(str,#cur-1).
END LOOP IF #cur GT CHAR.LENGTH(str).
LIST.

If this doesn't do what you want then you need to elaborate and illustrate with an example of your intended result.


On Wednesday, September 21, 2016 at 2:05:37 PM UTC-4, Rich Ulrich wrote:
> On Wed, 21 Sep 2016 07:51:37 -0700 (PDT), cedalexandre82@gmail.com
> wrote:
> 
> >Hi,
> >
> >I'm trying to find out how to detect repeated characters (such as "aaaaaa", "gggggg", "ZZZ", ...)
> >
> >Anyone has got an idea how to do it?
> >
> 
> Obviously, you have to have a loop in which you pick up each
> character and compare it to something - first character or 
> previous character, it seems - until you reach the end of
> the non-blank part of the string.  Consider LTRIM?  RTRIM? 
> 
> If you only want to know that some character is repeated,
> somewhere, you compare each to the previous, and quit
> once there is a single match. 
> 
> What you do in detail depends on what you want to know and
> report. 
> 
> -- 
> Rich Ulrich

0
David
9/21/2016 7:17:43 PM
Reply: