i have a value coming from db in string or number ...
# cfml-general
s
i have a value coming from db in string or number an i am trying to increment, for numbers, i can use incrementValue, but values like 15A, i want to detect if its not a number, alleast update the string from 15A to 15B so it ends up unique, possible?
m
definitely not possible. /s (this question really belongs in cfml-beginners) What have you researched about the various isFunctions (such isNumeric)? If a letter like A and B can be represented using ASCII values, how would you increment it?
a
Agreed re beginners channel. But before posting again then, go do some research and write your first pass at DIY, and then come back to us when you get stuck. The answer to the question "is it possible?" is always "yes". But not if you don't actually write some code.
s
i was thinking of adding a operator to check if the isnumeric returns me false and then, my only obstacle to replace the last value of the string like A with B which is where i am not sure how can i
because i am counting possibilities, not always it ends up with A
it can end up with Z also
a
OK so how does Z increment?
s
it seems its not a doable because there can be different permutations
a
And do you wqant to increment or "make unique". Those are not the same things.
s
i have to find a different approqaqch
make unique is the best
m
What is the value you are dealing with... what defines it
a
Back up. Forget about arriving at technical implementations for a problem that doesn't yet seem very well defined.
What problem are you trying to solve?
m
does it have to look incremented? or can it just be a GUID
s
i want to end up unique values,
but incremental uniqueness
a
nono, you are still talking solution here. What is the problem
m
@Simone, ever use ChatGPT?
s
very funny, are you ocking me because of my english
a
If you really just want a unique number per database row then you can use
myquery.currentrow
, which is literally the index of the row. But as others have said you have not really explained the problem.
m
No Simone, I'm asking if you have ever used it. It's a great tool for asking programming questions, it will generate commented code.
I ran your prompt through it, and I got this cf code:
Copy code
<cfscript>
    var input = "51A";  // try 51 or 51A
    var position = REFind("[A-Za-z]", input, 1, true); // find the first position of any letter

    if (position.len[1] > 0) { // if a letter was found
        var numberPart = Left(input, position.pos[1] - 1); // get the number part
        var stringPart = Mid(input, position.pos[1], Len(input)); // get the string part

        if (Len(stringPart) > 0) {
            var lastChar = Right(stringPart, 1);
            var newChar = chr(asc(lastChar) + 1);
        }

        WriteOutput("Incremented value: " & numberPart&newChar);
    } else {

        input++;
        WriteOutput("Incremented value: " & input);
    }
</cfscript>
you can toy around with this in trycf.com
s
@mrtom not sure what is chatGPT, i never used it, over here we are quite far behind all thse techs but i tried your code and modified a bit and it seems increment is working as far good
b
@Simone, updating by "incrementing" from 15A to 15B is not optimal. As you yourself say, there is 15Z to think about. I would suggest that - in a case such as 15A - you increment by appending the last character. https://trycf.com/gist/a6ac3be57a61ee104b44cade77c6e380/lucee6-beta?theme=monokai
m
@Simone How big is this field? Have you can try using datetime string (i.e. DateTimeFormat(now(), "yyyymmddhhnnssl"). Please notice that last character in the mask is a lower case L, which is for milliseconds)? Since you mentioned that this value is coming from the db, can you make this field unique in your database table definition? So, you have a try/catch block in your codes to catch for the db error. If it is always a number followed by an alphabet, does the most recently inserted record always have the latest sequence? No idea about the structure of your db table; does it have an auto-incremented PK field or a datetime field with the creation date? If so, you can do a max on that field and then get the associated row. In the resultset of that latest inserted row, you will have the most recent string/number. Compare your newly generated string/number against the most recent one (i.e. check if your number is smaller than the most recent one and/or if your new alphabet comes before or after the most recent one)