How to create list of unique 3 character alphabet code. If i pass "AAA" and 2, it should return "AAB", "AAC". 2 unique codes after "AAA"
Discussion posts and replies are publicly visible
Random != Unique.
Very curious as to why you would need to do this but here is some code to generate a string variable length with random characters. Not unique though. You should look into UUIDs if you want to achieve this.
a!localVariables( local!character: "A", local!numberOfCharactersToAppend: 2, local!allCharacters: "ABCDEFGHIJKLMNOPQRSTUVWXYZ", local!charactersToAppend: a!forEach( items: enumerate(local!numberOfCharactersToAppend), expression: a!localVariables( local!randomIndex: rounddown((rand() * 26 + 1), 0), mid( local!allCharacters, local!randomIndex, 1 ) ) ), concat( local!character, joinarray(local!charactersToAppend, "") ) )
Thanks all for the response. venkat Avuluri If i pass "AAZ" and 2. its returning "AA[" and "AA\" where it should be "ABA" and "ABB"
Yup it's gonna give you that .I just pasted sample code how it needs be done
Mathieu Drouin said:Very curious as to why you would need to do this
Same here ,Can I know why you need this in the first place . We can suggest alternatives
When i add new records to the editable grid, it saves in database with generated unique code in a column. If i create 3 new rows, it needs 3 new codes to be generated to assign it to each row. There may situations where end user can delete already added row and at that time we need to recreate codes and assign to each row accordingly.
Are you sure this is a good way to solve the problem of primary key values?
shivas3219 said:it saves in database with generated unique code in a column
In this case we can suggest multiple approaches. The approach you mentioned , I don't suggest it for various reasons .
Stefan Helzle This is not a primary key but unique code to identify the record and some other stuff. I would have created a dictionary of unique 3 char code but it may degrade performance.
Sorry, but this will generate conflicting values as soon as multiple users create rows. There is a good reason for why we use the database to create these values.
For what purpose do you need these values? And why not just use the numeric primary key values from the database?
There will be only user who would be updating it for now. I agree that it may create some conflicting values with multiple users. This code is being used in mapping to other tables.
Well ... then, good luck with this design.
Stefan Helzle could you please suggest how to get number of unique codes I need, using above design gives me values till AAZ, but I need further codes for my requirement