matmat
matmat

Reputation: 85

Compare strings in Intersystems Cache Objectscript

Given:

2 strings strA, strB

I want:

To perform a comparison between them and return <0, =0 or >0, in Intersystems Cache ObjectScript.

So far:

I have found a function in the documentation that fulfills my needs StrComp. Unfortunately, this function is not part of Cache ObjectScript, but from Caché Basic.

I have wrapped the function as a classMethod of an utility class:

ClassMethod StrComp(
    pstrElem1 As %String,
    pstrElem2 As %String) As %Integer [ Language = basic ]
{
    Return StrComp(pstrElem1,pstrElem2)
}

Is this approach recommended? is there any function available?

Thanks in advance.

Upvotes: 4

Views: 1728

Answers (3)

fge
fge

Reputation: 121710

You can use that if you want pure ObjectScript; it supposes that you really want to do something like Java's Comparable<String>:

///
/// Compare two strings as per a Comparator<String> in Java
///
/// This method will only do _character_ comparison; and it pretty much
/// assumes that your Caché installation is Unicode.
///
/// This means that no collation order will be taken into account etc.
///
/// @param o1: first string to compare
/// @param o2: second string to compare
/// @returns an integer which is positive, 0 or negative depending on
/// whether o1 is considered lexicographically greater than, equal or
/// less than o2
ClassMethod strcmp(o1 as %String, o2 as %String) as %Integer
{
    #dim len as %Integer
    #dim len2 as %Integer
    set len = $length(o1)
    set len2 = $length(o2)
    /*
     * Here we rely on the particularity of $ascii to return -1 for any
     * index asked within a string literal which is greater than it length.
     *
     * For instance, $ascii("x", 2) will return -1.
     *
     * Please note that this behavior IS NOT documented!
     */
    if (len2 > len) {
        len = len2
    }

    #dim c1 as %Integer
    #dim c2 as %Integer

    for index=1:1:len {
        set c1 = $ascii(o1, index)
        set c2 = $ascii(o2, index)

        if (c1 '= c2) {
            return c1 - c2
        }
    }

    /*
     * The only way we could get here is if both strings have the same
     * number of characters (UTF-16 code units, really) and are of
     * equal length
     */
    return 0
}

Upvotes: 2

Brandon Horst
Brandon Horst

Reputation: 2070

It's a bit unclear exactly what you want this string comparison to do, but it appears that you're looking for either the follows ] or sorts after ]] operator.

Docs (taken from here):

  • The binary follows operator (]) tests whether the characters in the left operand come after the characters in the right operand in ASCII collating sequence.
  • The binary sorts after operator (]]) tests whether the left operand sorts after the right operand in numeric subscript collation sequence.

The syntax looks weird but it should do what you need.

if "apple" ] "banana" ...
if "apple" ]] "banana" ...

Upvotes: 3

DAiMor
DAiMor

Reputation: 3205

It is possible to use different languages in your code, if it solve your task, why not. But you have to notice that not all languages is works on server's side. JavaScript still client's side language, and can't be used in such way.

Upvotes: 1

Related Questions