Skip to Content

How to Count number of words in a file....

Hi Experts,

I have uploaded the text file, from the application server, like this:

call function 'GUI_UPLOAD'

exporting

filename = LV_ip_FILENAME

tables

data_tab = LT_FILETABLE.

The text file contains some number character words.... like "sap labs india..... "

Now, I wanted to count number of words in an internal table LT_FILETABLE.... can anybody help me?

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

3 Answers

  • Best Answer
    Posted on Dec 12, 2011 at 05:41 AM

    Try this logic.

    DATA: g_regex(5)       TYPE c VALUE '\S+',
          g_count          TYPE i,
          gt_words         TYPE match_result_tab.
    
    PARAMETERS:g_string   TYPE string.
    
    FIND ALL OCCURRENCES OF REGEX g_regex IN g_string RESULTS gt_words.
    
    DESCRIBE TABLE gt_words LINES g_count.
    
    WRITE g_count.
    

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Venkatesha N

      Hi,

      Special Characters in Regular Expressions

      The following tables summarize the special characters in regular expressions:

      Escape character

      Special character Meaning

      Escape character for special characters

      Special character for single character strings

      Special character Meaning

      . Placeholder for any single character

      C Placeholder for any single character

      d Placeholder for any single digit

      D Placeholder for any character other than a digit

      l Placeholder for any lower-case letter

      L Placeholder for any character other than a lower-case letter

      s Placeholder for a blank character

      S Placeholder for any character other than a blank character

      u Placeholder for any upper-case letter

      U Placeholder for any character other than an upper-case letter

      w Placeholder for any alphanumeric character including _

      W Placeholder for any non-alphanumeric character except for _

      [ ] Definition of a value set for single characters

      [^ ] Negation of a value set for single characters

      [ - ] Definition of a range in a value set for single characters

      [ [:alnum:] ] Description of all alphanumeric characters in a value set

      [ [:alpha:] ] Description of all letters in a value set

      [ [:blank:] ] Description for blank characters and horizontal tabulators in a value set

      [ [:cntrl:] ] Description of all control characters in a value set

      [ [:digit:] ] Description of all digits in a value set

      [ [:graph:] ] Description of all graphic special characters in a value set

      [ [:lower:] ] Description of all lower-case letters in a value set

      [ [:print:] ] Description of all displayable characters in a value set

      [ [:punct:] ] Description of all punctuation characters in a value set

      [ [:space:] ] Description of all blank characters, tabulators, and carriage feeds in a value set

      [ [:unicode:] ] Description of all Unicode characters in a value set with a code larger than 255

      [ [:upper:] ] Description of all upper-case letters in a value set

      [ [:word:] ] Description of all alphanumeric characters in a value set, including _

      [ [:xdigit:] ] Description of all hexadecimal digits in a value set

      a f v Diverse platform-specific control characters

      [..] Reserved for later enhancements

      [==] Reserved for later enhancements

      u2192 More

      Special characters for character string patterns

      Special character Meaning

      Concatenation of n single characters

      {n,m} Concatenation of at least n and a maximum of m single characters

      {n,m}? Reserved for later enhancements

      ? One or no single characters

      • Concatenation of any number of single characters including 'no characters'

      *? Reserved for later enhancements

      + Concatenation of any number of single characters excluding 'no characters'

      +? Reserved for later enhancements

      | Linking of two alternative expressions

      ( ) Definition of subgroups with registration

      (?: ) Definition of subgroups without registration

      1, 2, 3 ... Placeholder for the register of subgroups

      Q ... E Definition of a string of literal characters

      (? ... ) Reserved for later enhancements

      for more details please refer the following,

      [http://help.sap.com/abapdocu_70/en/ABENREGEX_SYNTAX_SIGNS.htm]

      [http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/902ce392-dfce-2d10-4ba9-b4f777843182?QuickLink=index&overridelayout=true]

      Thanks,

      Renuka S.

  • author's profile photo Former Member
    Former Member
    Posted on Dec 12, 2011 at 05:55 AM

    Hi friend,

    Read the values from the internal table into a string variable. Then you can use the STRLEN string function for counting the number of words in that string.

    Use other string options if you want to manipulate the string further.

    Sample code :

    data : a TYpe i.

    data : x_string TYPE string.

    READ TABLE t_scarr into x_string INDEX 1.

    write : x_string.

    a = strlen( x_string ).

    write : a.

    I think this will work if you have any issues please let me know.

    Thanks,

    Sri Hari

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Dec 12, 2011 at 05:38 AM

    While reading the internal table contents, write the logic by making use of the word separators like space,dot(.),comma(,) etc to find out the solution for your case.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.