Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Determining field length

Former Member
0 Kudos

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

1 ACCEPTED SOLUTION

Former Member
0 Kudos

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.

16 REPLIES 16

Former Member
0 Kudos

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.

Former Member
0 Kudos

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

Former Member
0 Kudos

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.

0 Kudos

Hi Prammenthiran,

You got it right partially.

I am using split using a work area. Please find below the code:

SPLIT p_tab-record AT p_sep INTO

wa_data-order

wa_data-text

wa_data-order_num

wa_data-comp_code

wa_data-cost_center

wa_data-req_comp_code

w_profit

wa_data-pers_resp

wa_data-ext_order_num

wa_data-req_cost_center

wa_data-date_ends

wa_data-status(2)

wa_data-lock_ind

wa_data-asset_class

wa_data-cap_date

wa_data-deprec_cc

wa_data-department

wa_data-applican

wa_data-telephopr

wa_data-appdate

wa_data-startwk

wa_data-category

wa_data-setreceiv

wa_data-setperce

w_equival

wa_data-amount

wa_data-settype

wa_data-fromper

wa_data-fromfy

wa_data-toper

wa_data-tofy

wa_data-ma_ist

wa_data-ma_plan

wa_data-quadrant

wa_data-ref_ist

wa_data-ref_plan

wa_data-region

wa_data-tn_ist

wa_data-tn_istp

wa_data-tn_plan

wa_data-tn_plang.

Will it not be tedious to search each and every character for a '#' and then do the string length calculation???

Please suggest...

Cheers,

Raghav.

0 Kudos

I find that the "FIND ALL OCCURRENCES" function is handy. For example, the untested code below would search the string for "#", and populate a table containing the location of the "#". You could then just read the results table to find th 35th value.

find all occurrences
   of "#"
    in p_v_editor_text
    ignoring case
    results l_t_mr_objects.

0 Kudos

Hi Jerry,

Does 'find all occurrences' work in version 4.6????

Its giving me a syntax error..

Can you please guide???

0 Kudos

I am using a BW 7.0 (NW2004S) system.

The following works on my system. That would be a shame if it didn't on yours. It is a very nice tool.

REPORT zjc_temp.

DATA:
  g_c(80)    TYPE c    VALUE 'abc##123###xyz',
  l_t_mr     TYPE match_result_tab,
  l_s_mr     TYPE match_result.

FIND ALL OCCURRENCES OF '#'
  IN g_c
  RESULTS l_t_mr.


LOOP AT l_t_mr INTO l_s_mr.
  WRITE: / l_s_mr-offset,l_s_mr-length.
ENDLOOP.

WRITE sy-subrc.

Prints


        3           1
        4           1
        8           1
        9           1
       10           1

0 Kudos

Jerry...

it doesnt work...!!! it indeed is a shame...!!!!!

i hope we do not have to Include anything (like TABLE....or a CLASS...) prior to using this feature....do we???? just a thought......

0 Kudos

I don't know why Chandrasekhar's code won't work for you in the same way.

Make sure you give him points. It is his code.

REPORT zjc_temp.

DATA:
  g_c(80)    TYPE c    VALUE 'abc##123###xyz',
  itab       TYPE TABLE OF string,
  g_s        TYPE string.

SPLIT g_c AT '#' INTO TABLE itab.

LOOP AT itab INTO g_s.
  WRITE:/ sy-tabix,g_s.
ENDLOOP.

0 Kudos

Hi Jerry,

I want to find the precise position of the '#'s occuring in a string.

In the code mentioned (abc##123###xyz), the # occurs at positions 4,5,9,10 & 11.

while, the output is:

1 abc

2

3 123

4

5

6 xyz

so, please understand that this is not fulfilling my requirements.

anyways, will award chandrashekhar surely.

Cheers,

Raghav.

0 Kudos

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

Former Member
0 Kudos

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

Clemenss
Active Contributor
0 Kudos

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

Former Member
0 Kudos

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.

Former Member
0 Kudos

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.

0 Kudos

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.