
Is it good to write like that? No, I don't think so and you all know the reason
But did I have fun writing like that? Hell yeah!!
out->write( data = VALUE string_table(
" Declaring the string & extracting the words from the string
LET sentence = `ABАP is excellent `
words = VALUE string_table(
FOR rawword IN NEW cl_abap_regex( pattern = '(\b\w+\b)'
)->create_matcher( text = sentence )->find_all( )
( substring( val = sentence off = rawword-offset len = rawword-length ) ) )
" Filling the below additional value using base as the below for loop will
" only get executed max words times,
" But our output needs max words + 1 lines
IN BASE VALUE string_table( ( |Number of Words: { lines( words ) }| ) )
" Loop all the words and extract the unique characters
FOR word IN words ( |Number of unique characters in the word: { word } -
{ lines( NEW cl_abap_regex( pattern = '(\w)(?!.*\1)'
)->create_matcher( text = word )->find_all( ) ) }| )
) ).
out->write( data = VALUE string_table(
" Data declaration & finding the total no of words
LET sentence = `ABАP is excellent `
totalWords = count( val = sentence regex = `(\b\w+\b)` )
" Filling the base table with Total no of words as we will loop words times
IN BASE VALUE string_table( ( |Number of Words: { totalWords }| ) )
" Looping total words for unique characters count
FOR i = 1 UNTIL i > totalWords (
" Getting the word
CONV #( LET word = match( val = sentence regex = '(\b\w+\b)' occ = i )
" Getting the count of unique characters
IN |Number of unique characters in the word: { word } -
{ count( val = word regex = `(\w)(?!.*\1)` ) } | ) ) ) ).
" Get the total no of words
SPLIT condense( sentence ) AT space INTO TABLE DATA(words).
" Output
out->write( data = VALUE string_table(
" Filling the base table with Total no of words
BASE VALUE string_table( ( |Number of Words: { lines( words ) }| ) )
" Looping words to find individual words unique character count
FOR word IN words ( |Number of unique characters in the word:
{ word } - { lines( VALUE string_table(
" Grouping characters -> group will auto sort and find the unique records
FOR GROUPS char_group OF char IN VALUE string_table(
" For filling character table to group it in the above statement,
" we need to loop string length times
FOR char_pos = 1 UNTIL char_pos > strlen( word )
" Now getting individual characters
( substring( val = word off = char_pos - 1 len = 1 ) ) )
" filling the character table for grouping
GROUP BY char ( char_group ) ) ) }| ) ) ).
DATA(sentence) = `ABАP is excellent `.
SPLIT condense( sentence ) AT space INTO TABLE DATA(words).
out->write( data = |Number of Words: { lines( words ) }| ).
LOOP AT words REFERENCE INTO DATA(word).
DATA(characters) = VALUE string_table( FOR i = 1 UNTIL i > strlen( word->* )
( substring( val = word->* off = i - 1 len = 1 ) ) ).
SORT characters.
DELETE ADJACENT DUPLICATES FROM characters.
out->write( data = |Number of unique characters in the word: { word->* }
- { lines( characters ) }| ).
ENDLOOP.
out->write( data = VALUE string_table(
LET sentence = `ABАP is excellent `
" Condensed sentence
condensedSentance = condense( sentence )
" Total no of spaces + 1 equals to total no of words
totalNoOfWords = count( val = condensedSentance sub = ` ` ) + 1
" Filling the base table with Total no of words
IN BASE VALUE string_table( ( |Number of Words: { totalNoOfWords }| ) )
" Looping words to find individual words unique character count
FOR wordNo = 1 UNTIL wordNo > totalNoOfWords
LET word = segment( val = condensedSentance index = wordNo sep = ` ` )
uniqueChars = VALUE string_table(
" Grouping characters -> group will auto sort and find the unique records
FOR GROUPS charGrp OF char IN VALUE string_table(
" For filling character table to group it in the above statement
FOR charPos = 0 UNTIL charPos = strlen( word ) ( word+charPos(1) ) )
" filling the character table with unique chars resulted after grouping
GROUP BY char ( charGrp ) )
" Filling the string table for showing the ouput
IN ( |Number of unique characters in the word: { word } - { lines( uniqueChars ) }| ) ) ).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
3 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 |