Converting the the first character to numbers.

asemberengasembereng Member Posts: 220
Hi,
i am face with a challenge of converting the first character of a datatype code to number.
For example
A556 will be 01556
B556 will be 02556

Basically taking the alphabets to number,
A==01
B==02
C==03
D==04
E==05 , etc
all the program should do is take the first character converts it to number then add it to the reminding characters.
Can some help me on this?
Thank you
«1

Comments

  • SavatageSavatage Member Posts: 7,142
    edited 2008-03-17
    NewCodeField := COPYSTR(YourCodeField,1,1)
    If NewCodeField = 'A'
    then begin
    NewCodeField := DELCHR(YourCodeField,'<',DELCHR(NewCodeField);
    YourCodeField := '01'+ NewCodeField;
    End;

    if NewCodeField = 'B'
    then Begin
    NewCodeField := DELCHR(YourCodeField,'<',DELCHR(NewCodeField);
    YourCodeField := '02'+ NewCodeField
    end;
    etc for c & d & e;


    Is this a Primary key field?

    I'm sure there is a prettier way.. :-#
  • garakgarak Member Posts: 3,263
    edited 2008-03-17
    if this field is not an primary key, or part of them. Than you can do following.
    YourTable.findset(true)
    repeat
      case copystr(YourTable."YourCodeField",1,1) of
        'A':
          begin
            YourTable."YourCodeField" := '01' + copystr(YourTable."YourCodeField",2,Strlen(YourTable."YourCodeField"));
            //or 01' + DELCHR(YourTable.YourCodeField,'<',DELCHR('A');
            YourTable.modify;
          end;
        'B':
          begin
            YourTable."YourCodeField" := '02' + copystr(YourTable."YourCodeField",2,Strlen(YourTable."YourCodeField"));
            YourTable.modify;
          end;  
        'C':
          begin
          end; 
      end;
    until YourTable.next = 0;
    
    Do you make it right, it works too!
  • SavatageSavatage Member Posts: 7,142
    I knew It =D> Nice 8)
  • garakgarak Member Posts: 3,263
    #-o another one, that savatage are faster. fuck, i should press the "turbo" button on my old 386 computer :-D
    Do you make it right, it works too!
  • jreynoldsjreynolds Member Posts: 175
    To avoid writting code that is specific to each letter of the alphabet you can use
    STRPOS('ABCDEFGHIJKLNOPQRSTUVWXYZ',COPYSTR(OriginalCode,1,1))
    
    to convert the leading letter to an integer. You can then format the integer and go from there.
  • asemberengasembereng Member Posts: 220
    I understand what savatage said but jreynolds i am a bit confuse about you said, would you mind explaining further? thanks
  • SavatageSavatage Member Posts: 7,142
    edited 2008-03-17
    garak wrote:
    i should press the "turbo" button on my old 386 computer :-D

    Always wondered why you wouldn't want that button pressed? :-k
  • garakgarak Member Posts: 3,263
    than my old DOS Navision application is to fast :-D
    Do you make it right, it works too!
  • ara3nara3n Member Posts: 9,256
    Savatage wrote:
    garak wrote:
    i should press the "turbo" button on my old 386 computer :-D

    Always wondered why you wouldn't want that button pressed? :-k

    I wish to have the turbo button on laptops.

    My laptop runs very hot it's a HP NC 6230. The fan is on all the time. That way I can lower the CPU clock speed so it runs just like it's on battery.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • kinekine Member Posts: 12,562
    Hey guys, why you are doing it in this hard way? You need just one line of code... :mrgreen:
    b := FORMAT(a[1]-'A'+1,2,'<integer,2><filler character,0>')+COPYSTR(a,2);
    

    a - is variable of type Code with the original string.
    b - is variable of type Code with result

    Do you know how it works? I will let you some time to think about it, if you want I can describe it for the rest, who do not understand... :wink:
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • garakgarak Member Posts: 3,263
    to hard way --> he can learn loops, if or Case statemant and string function ;-)

    one line coding is for old navision coder :lol:
    Do you make it right, it works too!
  • SavatageSavatage Member Posts: 7,142
    That's why he has 6220 posts - he's really great at this Nav stuff \:D/
  • DenSterDenSter Member Posts: 8,305
    garak wrote:
    one line coding is for old navision coder :lol:
    Not old, just REALLY good :mrgreen:
  • Alex_ChowAlex_Chow Member Posts: 5,063
    garak wrote:
    #-o another one, that savatage are faster. fuck, i should press the "turbo" button on my old 386 computer :-D


    HAAHHAHA.... Oh my god... This has to be the post of the month! :lol:
  • Alex_ChowAlex_Chow Member Posts: 5,063
    kine wrote:
    Hey guys, why you are doing it in this hard way? You need just one line of code... :mrgreen:
    b := FORMAT(a[1]-'A'+1,2,'<integer,2><filler character,0>')+COPYSTR(a,2);
    

    a - is variable of type Code with the original string.
    b - is variable of type Code with result

    Do you know how it works? I will let you some time to think about it, if you want I can describe it for the rest, who do not understand... :wink:

    I understand the code. What I want some explaining on is how are you able to figure out that you can use a string variable with an array syntax.
  • kinekine Member Posts: 12,562
    DenSter wrote:
    garak wrote:
    one line coding is for old navision coder :lol:
    Not old, just REALLY good :mrgreen:

    Thanks Daniel, that you have taken my side... :mrgreen:

    I do not feel old... but it is true, that my first computer was Atari 800XE... :whistle:
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • asemberengasembereng Member Posts: 220
    Kine i am confuse can you explain what that simple line does?
  • kinekine Member Posts: 12,562
    asembereng wrote:
    Kine i am confuse can you explain what that simple line does?

    Ok, I will try.

    First some basics:

    Datatype Text (or Code) is defined as ARRAY of CHARs. Char is a datatype, which has size of one byte and it means that the values are 0-255. The char datatype can be used as number (0-255) or as a Character ('A', 'B', '$' etc.). Relation between character and number is, that each character has some ordinal number which is defined by ASCII table. It means, that e.g. 'A' = 65, 'B' = 66, 'Z'= 90 etc. You can use mathematical operators with both "notations" (number and character) - it means you can do e.g. 'A'-'B' = 1, if both operators are of type char. And this is the base of the expression.

    Ok, let's go:

    *** a[1]-'A'+1
    a[1] = first char from the "String" (variable) a - it is the first character in the text saved in variable a. Because Text (Code) datatype is ARRAY of chars, you can use indexing to read some component of the array... it means that a[1] is of type Char and it is the char on position 1...

    By substracting 'A' from any char (if we are using just uppercase characters from period A..Z), you will get the position of the character in the alphabet (A=0, B=1,...). And because you want to have A as 1, I am adding 1 to the result.

    ***FORMAT(Something,2,'<integer,2><filler character,0>')
    this will format the number in "Something" into text with length 2 filled with 0 from left. It means e.g. 1 is formated as '01' etc.

    ***+COPYSTR(a,2)
    add the rest of the text, without first character (take substring, starting on 2nd character, to the end)

    As you can see, it is about understanding the datatypes and some basic things from programming languages (arrays, string representation, ,,mathematical operations over the different datatypes).

    I hope that this description will help you to understand more deeply some things, and that it will not discourage you from NAV... 8)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • kinekine Member Posts: 12,562
    Alex Chow wrote:
    I understand the code. What I want some explaining on is how are you able to figure out that you can use a string variable with an array syntax.

    Because C/AL is based on Pascal, the "Text" and "Code" datatype is same as String in pascal. And String in Pascal is defined as ARRAY of CHAR... :-)

    For me it is obvious - I started to use Pascal 17 year ago on my first PC of 286 class... :-)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • garakgarak Member Posts: 3,263
    with format(a[1] - 'A' + 1) the programm do following:

    he use the first charakter of String a. This is an Letter. The Letter A has the AsCII Code 65. B has the Code 66 .....

    Now whe subtract from ASCII 65 the Value of 'A' (this is 65)
    So 65 - 65 + 1 is 1. With the second parameter ",2,'<integer,2><filler character,0>'" we say the lenght must be 2. with the 3 parameter wie say that if the lenght is < 2 we will out this wit 0 at begin
    Do you make it right, it works too!
  • garakgarak Member Posts: 3,263
    ah, forgotten to press Submit instead of Preview ](*,)
    Do you make it right, it works too!
  • kinekine Member Posts: 12,562
    Thanks garak for making this simple description... may be it will be much more understandable than my one... :whistle:
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • garakgarak Member Posts: 3,263
    Please
    Do you make it right, it works too!
  • David_SingletonDavid_Singleton Member Posts: 5,479
    Alex Chow wrote:
    I understand the code. What I want some explaining on is how are you able to figure out that you can use a string variable with an array syntax.

    Its much like += its one of those things that had been there all the time, (it used to be in the STX file I think) but took a while before everyone started to use it. The reason being fear that it would not work in the next version. But once NAV started using += in the base app, its seems acceptable for us to do it to.

    Unfortunately with the Text as array of CHAR, we now know that support for this undocumented "feature" will not be around in future versions of NAV. So we are all going to have to go around and do some code clean up I guess.
    David Singleton
  • kinekine Member Posts: 12,562
    Unfortunately with the Text as array of CHAR, we now know that support for this undocumented "feature" will not be around in future versions of NAV. So we are all going to have to go around and do some code clean up I guess.

    Why? This is common for all languages I know. Difference is jsut if text is array of char or wide char. But it is from basic definition of the text string... :-) I am not afraid that this will be not supported in next versions. Every language is using indexing to access elements of the string. 8)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • David_SingletonDavid_Singleton Member Posts: 5,479
    edited 2008-03-18
    kine wrote:
    Why? This is common for all languages I know. Difference is jsut if text is array of char or wide char. But it is from basic definition of the text string... :-) I am not afraid that this will be not supported in next versions. Every language is using indexing to access elements of the string. 8)

    Hmm I think you missunderstood. I didn't say you shoudln't use it. I use it my self. I just said it WILL NOT BE SUPPORTED in future versions of NAV.

    Its not NDA anymore, so we are allowed to say this right?
    David Singleton
  • kinekine Member Posts: 12,562
    Mhh I think you missunderstood. I didn't say you shoudln't use it. I use it my self. I just said it WILL NOT BE SUPPORTED in future versions of NAV.

    Its not NDA anymore, so we are allowed to say this right?

    But still I do not understand about what you are still talking... I do not know anything which can have impact to this... :-k
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • BeliasBelias Member Posts: 2,998
    1 personal question about this:
    To avoid writting code that is specific to each letter of the alphabet you can useCode:
    STRPOS('ABCDEFGHIJKLNOPQRSTUVWXYZ',COPYSTR(OriginalCode,1,1))
    to convert the leading letter to an integer. You can then format the integer and go from there.
    DOES THIS REALLY WORK? :shock:

    doesn't nav search ABCDEFGHIJKLNOPQRSTUVWXYZ in the substring that is e.g. 'A' or 'D'? :shock: :shock:
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • David_SingletonDavid_Singleton Member Posts: 5,479
    kine wrote:
    But still I do not understand about what you are still talking... I do not know anything which can have impact to this... :-k

    Impact to what? I think we are both lost here. :?:
    David Singleton
  • kinekine Member Posts: 12,562
    kine wrote:
    But still I do not understand about what you are still talking... I do not know anything which can have impact to this... :-k

    Impact to what? I think we are both lost here. :?:

    It seems... we can go through it elsewhere than on this forum... :wink:
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
Sign In or Register to comment.