Application Development and Automation 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: 
Read only

get integer values 0 and 1 without using numbers

schmelto
Active Participant
1,921

Hey there,

I want to make a program printing the numbers 1 to 100 without using any numbers or loops.

I also made a recursive function in ABAP.

But i have no clue hot to get 0 and 1 without using numbers in ABAP.

May you have an idea?

//example in Javascript

const one = +true; // This would return 1 (Unary and boolean operator)
const zero = +false; // This would return 0 (Unary and boolean operator)
// Using string operations apending 1 and 0 and 0 to form "100"
const hundredInString = one.toString() + zero.toString() + zero.toString();
// To save the day for not using the loop, we are using recursion

function.const printInRecursion = (index) => {   
  if (index <= parseInt(hundredInString)) {
    console.log(index);
    printInRecursion(index + one);
  }
  return
}
printInRecursion(parseInt(one))
6 REPLIES 6
Read only

former_member736527
Active Participant
1,782

calculate the length of an empty string with STRLEN to get 0.

Edit: You can also take advantage of SY-SUBRC or other system variables.

Read only

1,782

Excellent.

Ask Basis to set up instance 001.

DATA(one) = sy-sysid.
Read only

joltdx
Active Contributor
1,782

Cool, that sounds like fun! 🙂

How about something that counts something, would that be ok... like strlen?

DATA(zero) = strlen( '' ).
DATA(one) = strlen( 'a' ).
Read only

matt
Active Contributor
1,782

Many hundreds of years ago, I was learning to program on my first computer - a ZX81. Memory was limited, so the community worked out ways of conserving it.

A number took maybe 5 bytes. But PI (a keyword) was a constant, with a value encoded in ROM. So for 1 we had PI / PI. And for 0 we had PI - PI. Both of which used less memory. Which is important when you're trying to squeeze 3D Monster Maze into 16K.

From the sublime to the ridiculous

DATA zero TYPE i.
DATA(one) = zero - sign( zero - sign( 2 ) ).
SELECT * FROM a_huge_db_table INTO @data(oh_this_is_utterly_ludicrous).
DATA(one) = lines( oh_this_is_utterly_ludicrous ) / lines( oh_this_is_utterly_ludicrous ).
Read only

matt
Active Contributor
1,782

Bonus points if you know where "Oh, this is utterly ludicrous" comes from.

Read only

touzik_itc
Active Participant
1,782

Function distance() was created for that

DATA(zero) = distance( val1 = abap_true val2 = abap_true ).
DATA(one)  = distance( val1 = abap_true val2 = abap_false ).