Skip to Content
avatar image
Former Member

Determining field length

Hi,

I have a delimited file (seperated by #). There are about 40 fields in each record of the file, which are separated by # as mentioned above.Please note that it is not necessary that each of these fields be populated, which means that some fields might be left blank.

I have a requirement of validating the length of the 35th field, as to whether it exceeds a certain length (20 in this case).

Can anyone suggest how can i determine the length of the field, especially when we dont know whether the previous fields are filled or empty, which makes it impossible to determine the correct offset for the 35th field.

I have tried using the SPLIT command, and SY-SUBRC is set to 4 if any of the fields exceed the specified length. but i dont know WHICH field is exceeding its length. this is what i want to know precisely.

<REMOVED BY MODERATOR>

Cheers,

Raghav.

Edited by: Alvaro Tejada Galindo on Mar 19, 2008 5:10 PM

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

6 Answers

  • Best Answer
    avatar image
    Former Member
    Mar 25, 2008 at 07:00 AM

    Well firstly,you need to find out the value of the hexa-decimal character. Second if you are on Non-Unicode enabled system you can define a variable with the same value of type X, On unicode enabled system you might want to use predefined characters. As in Chandra's example he has used the same for TAB Character. Useful class for the same is CL_ABAP_CHAR_UTILITIES. As per what i understand, you need to find the length of the 35th field and considering "#" represents TAB Character, check below example:

    DATA: str TYPE string,
          itab TYPE TABLE OF string,
          wa TYPE string,
          len TYPE i.
          
    " Use the Definition as per your system(UNICODE/NON-UNICODE)
    * Unicode system definition
    DATA: tab TYPE char1 VALUE cl_abap_char_utilities=>horizontal_tab.  " TAB Character
    
    * Non-Unicide System
    DATA: tab(1) TYPE x VALUE '09'.  " TAB Character
    
    ....
    READ DATASET <file> INTO str.
    IF sy-subrc NE 0.
       EXIT.
    ELSE.
       SPLIT str AT tab INTO TABLE itab.
       READ TABLE itab INTO wa INDEX 35.  " Fetch Contents of 35th Field
       IF sy-subrc EQ 0.
          CLEAR len.
          len = STRLEN( wa ).
          IF len > 20.
             " Error Message
          ELSE.
             " Required Action
          ENDIF.
       ENIDF.
    ENDIF.

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Thanks for the brilliant suggestion eswar. it has solved my problem.

      Thanks a lot again, and also thanks to all other ppl who gave thier valuable suggestions.

      Cheers,

      Raghav.

  • avatar image
    Former Member
    Mar 19, 2008 at 01:00 PM

    Hi Raghavendra,

    How did u use the SPLIT command , try this

    data : itab type table of string.

    SPLIT V_string at CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB into table ITAB.

    Loop at itab.

    v_len = strlen( itab-string )

    if v_len GT 20.

    ***message that field exceeded length 20

    else.

    endif.

    endloop.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Mar 19, 2008 at 01:01 PM

    Hi,

    Try moving file contents to a internal table (to respective fields).

    In this way , u can easily determine the length.

    Hope this helps.

    Regards,

    Ramya

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Mar 19, 2008 at 01:02 PM

    Hi,

    Please tell me some more details about your internal table which has been used to upload the delimited file.

    Suppose you got all the records in a single field internal table, then you can try the below logic.

    1)Hope your file records will have 40 '#' Symbols.

    2)The point is you need to get the string in between 34th & 35th '#' symbols. And do whatever validation you want.

    3) you can write a logic to count the '#' symbols by checking the characters one by one.

    I am not sure whether I got your question correctly.

    Check & reply/reward if it gives any idea. Or provide some more details.

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      Well, you could always use "brute force" programming to get the positions.

      REPORT zjc_temp.
      
      DATA:
        g_c(80)    TYPE c    VALUE 'abc##123###xyz',
        g_char     TYPE c,
        g_len      TYPE i,
        g_offset   TYPE i.
      
      
      g_len = STRLEN( g_c ).
      
      WHILE g_offset < g_len.
        g_char = g_c+g_offset(1).
        g_offset = g_offset + 1.
        IF g_char = '#'.
          WRITE:/ g_offset.
        ENDIF.
      ENDWHILE.
      
      
               4
               5
               9
              10
              11
      

  • avatar image
    Former Member
    Mar 24, 2008 at 09:46 AM

    Hi,

    Try with describe statement

    DESCRIBE FIELD dobj

    [TYPE typ [COMPONENTS com]]

    [LENGTH ilen IN {BYTE|CHARACTER} MODE]

    [DECIMALS dec]

    [OUTPUT-LENGTH olen]

    [HELP-ID hlp]

    [EDIT MASK mask].

    Regards,

    IFF

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Mar 24, 2008 at 02:39 PM

    Hi Raghavendra satwik,

    I have a delimited file (seperated by #). There are about 40 fields in each record of the file, which are separated by # as mentioned above.Please note that it is not necessary that each of these fields be populated, which means that some fields might be left blank.

    say your record LV_RECORD looks like

    f1#f2###############################f33#f34#######

    lv_ofs = 0.
    do 34 times.
      if LV_RECORD+lv_ofs CA '#'.
        lv_ofs = SY-FDPOS + 1. 
      else.
    * raise error as  not enough field seoarators are present
      endif.
    enddo.
    * here the field no. 35 starts
    lv_len =  lv_ofs + 1.
    if  LV_RECORD+lv_len CA '#'.
      lv_len = sy-fdpos.
    endif.
    subtract  lv_ofs from lv_len.
    * Now you have the length of field no. 35
    

    Regards,

    Clemens

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Thanks for those very good suggestions.

      However, there is another small problem. The '#' that is present in the file is a hexadecimal one. While debugging, the system shows these '#' as '/h/' (i guess it is converting the hexadecimal to character).

      So the comparision using CA or EQ fails. i tried using FM 'NLS_STRING_CONVERT_TO_SYS' which converts hexadecimal to character, but it doesnot give me a '/h/' which is appearing the input file.

      Any suggestions from you experts????

      Thanks a lot again for your kind suggestions ppl.

      Cheers,

      Raghav.