Skip to Main Content

Count consecutive repetitive element in a sequence using XQuery

KaushikBoseFeb 24 2015 — edited Feb 25 2015

If the Sequence = [a a b c c c a d d e e e f g h h]

then the Output = [1 2 1 1 2 3 1 1 2 1 2 3 1 1 1 2]

Have tried to use recursion but no luck...Please Help..Thanks in Anticipation

Note: Using XQuery implementation 1.0

One of my failed implementation looks like:

declare function local:test($sequence,$count){

for $counter in (1 to count($sequence))

let $maxIndex := count($sequence)

return

if (matches(subsequence($sequence,1,$maxIndex)[$counter],subsequence($sequence,1,$maxIndex)[$counter + +1])) then let $count := $count + 1 return $count[last()]

else let $count := 1 return $count[last()]


};


It seems that as $counter is a sequence hence it returns a sequence in matches condition and hence this implementation fail

This post has been answered by odie_63 on Feb 24 2015
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked due to inactivity on Mar 25 2015
Added on Feb 24 2015
3 comments
1,386 views