1

I was working on this function Happy to practice control flow. Happy tests if a number is a happy number. I was trying also to learn error handling. So, I tried to catch if a user input is anything other than an integer. Especially if the input is a string. For this I tried to incorporate the scalar test here But I could not test it on the editor because if I try to enter any letters without quotes I get of course a VALUE ERROR: Undefined name.

Also, Mastering APL book warns about using Quad for data input.

What is the correct way to test if an input is a string? I assume that the user will enter letters without quotes.

(I'm using Dyalog APL 17.0)

(I took the code about how to split the number into its digits from here?)

Happy
 'Enter a Number'
 N←⎕
 N2←N
 :If N<0
     N←|N
     'You entered'N2
     'Only positive numbers can be happy'
     'We will check if'N'is happy'
     N2←N
 :EndIf
 f←10⊥⍣¯1⊢
 D←f N
 N←+/D×D
 ⍝:If 0≢⊃0⍴⊂N
 ⍝:OrIf N≢⌊N
 ⍝    'You entered: 'N'Enter an integer'
 ⍝    →0
 ⍝:EndIf
 :If N=1
     '***************'
     'Yes'N2'is happy!'
     '***************'
 :Else
     :While N≠1
         N←+/D×D
         D←f N
         N←+/D×D
         'N is:'N
         :If N=4
             '**********************************'
             'Sorry!'N2'is not a happy number'
             '**********************************'
             :Leave
         :EndIf
         :If N=1
             '**********************'
             'Yes!'N2'is a happy number'
             '**********************'
         :EndIf
     :EndWhile
 :EndIf

2 Answers 2

1

Updated answer

Without further ado, here's a suggested solution:

 Happy;ok;N;N2
 'Enter a Number'
 ok←0
 :While ~ok
     N2←N←⍞  ⍝ N2=original input
     ok←∧/N∊⎕D,'¯.'  ⍝ allow high minus for negatives and decimal separator
     (~ok)/'Please enter numeric data!'
 :EndWhile
 N←2⊃⎕VFI N  ⍝ result of ⍞ will be text, so make it numeric
 N←⍬⍴N       ⍝ make it a scalar 
 :If N<0
     N←|N
     'You entered'N2
     'Only positive numbers can be happy'
     'We will check if'N'is happy'
     N2←N
 :EndIf
 f←10⊥⍣¯1⊢
 D←f N
 N←+/D×D

 :If N=1
     '***************'
     'Yes'N2'is happy!'
     '***************'
 :Else
     :While N≠1
         N←+/D×D
         D←f N
         N←+/D×D
         'N is:'N
         :If N=4
             '**********************************'
             'Sorry!'N2'is not a happy number'
             '**********************************'
             :Leave
         :EndIf
         :If N=1
             '**********************'
             'Yes!'N2'is a happy number'
             '**********************'
         :EndIf
     :EndWhile
 :EndIf

Remarks

  • Legrand is right about not using , use instead. This gives you the "untranslated" string so that you can work with it. Using the system to evaluate user input (without error-trapping) could open the doors to all sorts of attacks. (Documentation here)

  • I used ⎕VFI (which is a Dyalog-specific system-function, I think) that verifies input. This is preferreable to which (again) could open doors...(Documentation here])

5
  • „Digits“. There‘s also {Quad}A for „Alphabet“
    – MBaas
    Oct 30, 2018 at 11:52
  • I was confused, so that's not the D I used in the function as D←fN where I calculate the digits of N?
    – zeynel
    Oct 30, 2018 at 12:27
  • ok,I had replied from my phone...but I see additional things that should be mentioned. Will update my teply later today ;)
    – MBaas
    Oct 30, 2018 at 12:37
  • ok, thanks. I tried it. It works but it catches numbers too and the rest of the function is not executed.
    – zeynel
    Oct 30, 2018 at 12:42
  • Thanks! I'm studying your answer. Ok, so I thought that Quote Quade took only string input. But I see that the user can enter numeric input but Quote Quade converts it to string: chars←⍞ 123 chars 123 +/chars DOMAIN ERROR.
    – zeynel
    Oct 31, 2018 at 9:03
1

You should be using (Character Input; documentation) rather than . will give you the literal text which the user entered.

input←⍞

You can then use ⎕VFI (Verify and Fix Input; documentation) on the result to check for numeric input. It will give you a vector of two vectors. The first is Boolean and indicated for each "word" in the argument. The second vector is numeric containing one number per word. Invalid words become zero.

(valid values)←⎕VFI input

So if you are looking for a single number check the validity vector, and then you can check for being an integer:

:If valid≢,1
:OrIf values≢⌊values
    'You entered: 'N'Enter an integer'
    →0
:EndIf

Finally you can extract N:

N←⊃values

Some unrelated advice

  1. Remember to localise your variables by listing them at the top, each preceded by a ;.
  2. Use :Else for the rest of the program instead of →0 to avoid jumping.
  3. Similarly, you can avoid jumping with :Leave by changing :EndWhile to :Until N=4.
  4. You code may be easier to read if you state your conditions in the positive.
  5. To avoid unwanted spacing, concatenate your output (with ,) rather than stranding (juxtaposing) it.
  6. Precede output with ⎕← to make it easier to spot and increase clarity.
 Happy;input;valid;values;N;N2;f;D
 ⎕←'Enter a Number'
 input←⍞
 (valid values)←⎕VFI input
 :If valid≡,1
 :AndIf values≡⌊values
     N←⊃values
     N2←N
     :If N<0
         N←|N
         ⎕←'You entered',N2
         ⎕←'Only positive numbers can be happy'
         ⎕←'We will check if',N,'is happy'
         N2←N
     :EndIf
     f←10⊥⍣¯1⊢
     D←f N
     N←+/D×D
     :If N=1
         ⎕←'***************'
         ⎕←'Yes',N2,'is happy!'
         ⎕←'***************'
     :Else
         :While N≠1
             N←+/D×D
             D←f N
             N←+/D×D
             ⎕←'N is:',N
             :If N=4
                 ⎕←'**********************************'
                 ⎕←'Sorry!',N2,'is not a happy number'
                 ⎕←'**********************************'
             :EndIf
             :If N=1
                 ⎕←'**********************'
                 ⎕←'Yes!',N2,'is a happy number'
                 ⎕←'**********************'
             :EndIf
         :Until N=4
     :EndIf
 :Else
     ⎕←'You entered: ',input,' Enter an integer'
 :EndIf
1
  • That's great! Thanks. Especially for the unrelated advice. I'll read it carefully tomorrow.
    – zeynel
    Oct 30, 2018 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.