Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dictionaries in ABAP. How?

How to translate this snippet of executable pseudo code into ABAP?

phone_numbers = {
    'hans': '++498912345',
    'peter': '++492169837',
    'alice': '++6720915',
}

# access
print (phone_numbers['hans'])

# add
phone_numbers['bernd']='++3912345'

# update
phone_numbers['bernd']='++123456'

if 'alice' in phone_numbers:
    print('Yes, alice is known')

# all entries
for name, number in phone_numbers.items():
    print(name, number)

Modern ABAP is possible up to 752, less chars, more upvotes :-)

P.S. BTW, up to now no one has added abap to pleac (Programming Language Examples Alike Cookbook)

like image 949
guettli Avatar asked Dec 04 '18 12:12

guettli


1 Answers

Well, how about the following solution?

REPORT ZZZ.

TYPES: BEGIN OF t_phone_number,
  name TYPE char40,
  number TYPE char40,
  END OF t_phone_number.

DATA: gt_phone_number TYPE HASHED TABLE OF t_phone_number WITH UNIQUE KEY name.

START-OF-SELECTION.
  gt_phone_number = VALUE #(
    ( name = 'hans' number = '++498912345' )
    ( name = 'peter' number = '++492169837' )
    ( name = 'alice' number = '++6720915' )
  ).

* access
  WRITE / gt_phone_number[ name = 'hans' ]-number.

* add
  gt_phone_number = VALUE #( BASE gt_phone_number ( name = 'bernd' number = '++3912345' ) ).

* update
  MODIFY TABLE gt_phone_number FROM VALUE #( name = 'bernd' number = '++123456' ).

  IF line_exists( gt_phone_number[ name = 'alice' ] ).
    WRITE / 'Yes, Alice is known.'.
  ENDIF.

* all entries
  LOOP AT gt_phone_number ASSIGNING FIELD-SYMBOL(<g_phone_number>).
    WRITE: /, <g_phone_number>-name, <g_phone_number>-number.
  ENDLOOP.
like image 180
Jagger Avatar answered Sep 30 '22 16:09

Jagger