'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?
kdb


Solution 1:[1]

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

Solution 2:[2]

You can use the in-built uppercase alphabet .Q.A to achieve what you want:

{all x in .Q.A} 

This lambda will return 1b if the input string consists only of capital letters and false otherwise.

Solution 3:[3]

Start by removing some misapprehensions.

  1. lower returns the lower case of its argument.
  2. show displays and returns its result. You can use it as you have to ensure the function’s result is printed on the console, but it is not required as, for example return would be in JavaScript.
  3. If you have a boolean as the result of a test, that can just be your result.
  4. Compare strings for equality using Match ~ rather than Equals

That said, two strategies: test for lower-case chars .Q.a, or convert to upper case and see if there is a difference.

q){not any x in .Q.a}"AAA_123"
1b
q){not any x in .Q.a}"AaA_123"
0b

The second strategy could hardly be simpler to write:

q){x~upper x}"AaA_123"
0b
q){x~upper x}"AAA_123"
1b

Together with the Apply . operator, the Zen monks construction gives you fast ‘point-free’ code – no need for a lambda:

q).[~] 1 upper\"AaA_123"
0b
q).[~] 1 upper\"AAA_123"
1b

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anton Dovzhenko
Solution 2 kylebonnes
Solution 3 SJT