Search code examples
kdb

Checking whether values are Upper case or lower case


I'm trying to build a function in q/kdb+ that can detect whether the string passed to the function is Upper case (True) OR lower case (false)

I have done several attempts but I'm constantly hitting a road block Here's my function, would appreciate the help. TIA

isAllCaps: {[input] if[input = lower; show 0b; show 1b]}

The above function basically takes an input as specified. It then checks from the if statement whether it is lower, if it is lower then it should return a false(0b), if not then return a true (1b). Really struggling with something so simple here. I'm just getting the following error:

evaluation error:

type

  [1]  isAllCaps:{[input] if[input = lower; show 0b; show 1b]}
                                   ^

  [0]  isAllCaps"a"

I have also tried other methods but only certain inputs were coming out to be successful. For instance:

isAllCaps: {[x] if[ x = upper type 10h; show 1b; show 0b]}

This equates to if x is upper type string, show true, else show false. Again, getting a type error. Idk why? All the tests are in strings i.e., "John_Citizen" etc. etc.

EDIT Tried this but I'm getting 2 outputs.

isAllCaps: {[L] if[L = lower L; show 0b; show 1b] } Am I missing something?

Solution

  • Try following code:

    isAllCaps: {[L] show L~upper L};
    

    It shows 1b if string is uppercase, and 0b otherwise.

    There are 2 mistakes in you code

    1. ~ should be used to compare strings. = does character-wise comparison. I.e. "aa"~"aa" gives 1b, but "aa"="aa" gives 11b
    2. Use if-else, instead of if. See 10.1.1 Basic Conditional Evaluation for more details