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: 

Efficient way to convert CHAR to INT and sort an internal table???

former_member214878
Active Participant
0 Kudos

Hello Guys,

I came across a situation where I need couple of alternatives to move ahead -

I have a Standard Internal table (I can not make it SORTED/HASHED --"Couple of constraints") - in an internal table there are only 2 columns -

1st Column - Offset - CHAR 3 (With or without minus sign)

2nd Column - Quantity - Packed value (Any value with/without decimal)

e.g.

Question  - I want to sort this internal table based on GL_OFFSET in integer format and NOT Character format.

What would be the efficient way to do a sort in your opinion.....

PS: This internal table might contain huge data at times, so several looping must be avoided.

15 REPLIES 15

karun_prabhu
Active Contributor
0 Kudos

Hello Ravindra Sonar.

     Either change the data type of gl_offset or move the data to an integer variable.

Regards.

0 Kudos

Hello Arun,

You mentioned most obvious thing but what about '-'  minus sign with the data ?

I can not change the type to integer with sign..

this is not a correct option.

0 Kudos

I feel the most obvious thing is sufficient enough.

Even if it has '-' sign,you can assign it to an integer variable.

What problem with that?

DATA: cvar(3),

            ivar TYPE i.

cvar = '-10'.

ivar = cvar.

0 Kudos


direct assignment like ivar = cvar is not allowed between C and I type objects.

Use WRITE CVAR to IVAR instead.

0 Kudos

Hello Arun,

Have a look on what Ashish wrote - he is having an exact point what I am concerned about.

If by any means I do sort this table having GL_OFFSET as a char field - no doubt my internal table will get sorted but it will be sorted by character by character ...

e.g. if I have -11, -2, -4, -111, -5, -8, -3, -8, -1111, -3, -2 then I am expecting as a result as -

EXPECTED ONE =  -2, -2, -3, -3, -4, -5, -8, -8, -11, -111, -1111

In case I go with your way I will get -

-11, -111, -1111, -2, -2, -3, -3, -4, -5, -8, -8

Hope I am clear what I am looking for ......

BR.

Rudra

0 Kudos

Ashish,

     It is permitted You can check it out with a sample.

0 Kudos

Rudra,

     You din't get my point.

     Assign your gl_offset to an integer variable and then sort the integer variable in descending order.

     For instance,

     DATA: BEGIN OF itab OCCURS 0,

         ivar TYPE i,

       END OF itab.

      DATA: c(4),

            ivar TYPE i.

      c = '-23'.

      itab-ivar = c.APPEND itab.

      c = '-2'.

      itab-ivar = c.APPEND itab.

     

      SORT itab DESCENDING by ivar.

      LOOP AT itab.

        WRITE: / itab-ivar.

      ENDLOOP.

0 Kudos

Hello Ashish/ Arun,

It is permitted as long as u are not bothered about that Minus sign.

In fact WRITE wont work on two different data types, if u have to move the data ignoring data type u should use MOVE statement instead of WRITE.

@Arun -

I was talking about the reply Asish made  -


Try appending '11' to it and see the sort results. 

. As it is of type char, sorting 1, 9, 11 will result into 1,11,9 because char sorting takes up each char at a time, so when it will search for 1 in all values, it will place all 1s together.

By the way-  I solved it using 2 loop   one loop I can avoid but I will work on it later..... See my next post for sample code .....................

No certain short cut available I guess ..........

0 Kudos

Rudra,

     I don't know why my communication cannot  reach you properly

     1) Declare an integer variable (say igl_offset TYPE I) in internal table LT_DATA.

     2) LOOP AT lt_data.

           LT_DATA-igl_offset = LT_DATA-gl_offset.

           MODIFY LT_DATA TRANSPORTING igl_offset.

         ENDLOOP.

     3) SORT lt_data BY igl_offset (Here you are sorting only integer values and not char values).

0 Kudos

You are using wrong test values.....include a '-1' in it and see the results.

shaik_sajid
Active Contributor
0 Kudos

Hi,

The First column even though its a character field since the values inside it are integers, when you sort, it will sort correctly.

Try the following code and you will understand.

TYPES: BEGIN OF ty_data,

gl_offset TYPE char3,

gl_qty TYPE p,

END OF ty_data.

data: lt_data TYPE STANDARD TABLE OF   ty_data,

ls_data LIKE LINE OF lt_data.

ls_data-gl_offset = '-5'.

ls_data-gl_qty = '5'.

append ls_data to lt_data.

ls_data-gl_offset = '1'.

ls_data-gl_qty = '25'.

append ls_data to lt_data.

ls_data-gl_offset = '0'.

ls_data-gl_qty = '30'.

append ls_data to lt_data.

ls_data-gl_offset = '9'.

ls_data-gl_qty = '10'.

append ls_data to lt_data.

ls_data-gl_offset = '-9'.

ls_data-gl_qty = '20'.

append ls_data to lt_data.

sort lt_data by gl_offset.

loop at lt_data INTO ls_data.

write:/ ls_data-gl_offset.

ENDLOOP.

Regards

Shaik

0 Kudos

Try appending '11' to it and see the sort results. . As it is of type char, sorting 1, 9, 11 will result into 1,11,9 because char sorting takes up each char at a time, so when it will search for 1 in all values, it will place all 1s together.

0 Kudos

Dear Ashish,

you are correct, I did not check that....

Regards

Shaik

venuarun
Active Participant
0 Kudos

Hi, Rudra

You can use this Function module to convert char into Int.

'MOVE_CHAR_TO_NUM'



Regrads

Arun VS

former_member202818
Active Contributor
0 Kudos

Hi Rudra,

Check this sample program

REPORT  zchar_int.

TYPES : BEGIN OF ty_char,

         value(3),

         END OF ty_char,

         BEGIN OF ty_int,

         value TYPE i,

         END OF ty_int.

DATA  : it_char TYPE TABLE OF ty_char,

         it_int  TYPE TABLE OF ty_int,

         wa_char TYPE ty_char,

         wa_int  TYPE ty_int.

START-OF-SELECTION.

   wa_char-value = '-1'.

   APPEND wa_char TO it_char.

   wa_char-value = '-21'.

   APPEND wa_char TO it_char.

   wa_char-value = '-111'.

   APPEND wa_char TO it_char.

   SORT it_char.

   MOVE it_char TO it_char.

   LOOP AT it_char INTO wa_char.

     wa_int-value = wa_char-value.

     APPEND wa_int TO it_int.

   ENDLOOP.

   SORT it_int by value.

   BREAK-POINT.