0

I want to convert from integer values to string characters as follows:

0 to "a"

1 to "b"

and so forth up to

26 to "z"

Is there a way to do this in e without a big case statement?

Note: e is strongly typed and it isn't possible to do any type of arithmetic on string values. There also isn't any char-like type.

Another node: To all you C/C++ hotshots who keep down-voting my question, this isn't as easy a problem as you might think.

Tudor Timi
  • 7,453
  • 1
  • 24
  • 53
  • No idea about "e" but in general `char(97 + n)` where `char` returns a char from an ascii/unicode code – Alex K. Nov 21 '14 at 16:12
  • I know how to do it in C/C++ thank you very much. Why answer/downvote to questions where you have no idea? – Tudor Timi Nov 21 '14 at 16:14
  • I did not downvote, my comment was a comment, not an answer. I have no idea if you knew this or not - if you didn't my comment could be quite useful, which after all is the point of this site. – Alex K. Nov 21 '14 at 16:17
  • @AlexK. Sorry, it must have been the guy who put the C++ answer up. – Tudor Timi Nov 21 '14 at 16:19
  • @Tudor, I guess probably because originally the question title didn't mention e, some people didn't realize what language it was about. – Yuri Tsoglin Nov 28 '14 at 08:16

2 Answers2

1

You can define a new enum type to correspond to the alphabet, and use the fact that enum values are backed by int values to transform a list of ints to a list of enums, or to a string.

Consider the following example:

<'
type chars : [a, b, c, d, e, f, g];

extend sys {
    run() is also {
        var l : list of int[0..6];
        var s: string = "";

        gen l keeping {it.size() == 5};
        print l;
        for each in l { print it.as_a(chars); };

        for each in l { s = append(s, it.as_a(chars)); };
        print s;
    };
};        
'>

The output of this example will be:

  l = 
0.      4
1.      0
2.      6
3.      4
4.      5
  it.as_a(chars) = e
  it.as_a(chars) = a
  it.as_a(chars) = g
  it.as_a(chars) = e
  it.as_a(chars) = f
  s = "eagef"

Note that you can assign custom values to elements in the enum. In that way, you can assign standard ASCII values to enum elements.

 type chars : [a=10, b=11, c=12, d=13, e=14, f=15, g=16];
Kalev
  • 1,158
  • 7
  • 17
  • This is also conceptually similar to a big `case` statement, because we have to define the big `enum` ourselves. It's more elegant though, because it allows for conversions in both directions. – Tudor Timi Nov 25 '14 at 12:38
1

You can do something like this:

{0c"a"+my_num}.as_a(string)

0c"a" denotes the ASCII value of the letter 'a'. And an as_a() conversion of a list of numbers (actually, bytes) into a string creates a string where each character has the ASCII value of the corresponding list element.

Yuri Tsoglin
  • 963
  • 4
  • 7
  • That's the answer I was looking for :D Is this mentioned in the documentation somewhere (I for one didn't find it)? – Tudor Timi Nov 28 '14 at 09:04
  • Yes, both points are mentioned in the documentation. The first one is mentioned in section about Literals and Constants ("Literal Character"). The second one is mentioned where as_a() and type conversions (between different combinations of types) are described. – Yuri Tsoglin Nov 28 '14 at 09:55