Verifying and Converting Input


What arrives from screens and keyboards (and frequently fles as well) is character - when the intention (and requirement of the code) is numeric we have the tedious chore of converting to numeric.

      ⍝ Monadic - simple default case
      ⎕vfi   ' 2 3     4.50'
 1 1 1  2 3 4.5

Easy enough,

Quite often it is sufficient to assume all will be well and either

      (1+⎕io)⊃ ⎕vfi   ' 2 3     4.50'
2 3 4.5
      ⊃(// ⎕vfi   ' 2 3     4.50')
2 3 4.5            

Where the former is most optimistic, and the latter a trifle more pragmatic (note that the parentheses are required because of Dyalog's function/operator ambivalence with Replicate/Reduce)

If the input could contain non-blank separators we can use ⎕VFI dyadically

      '£,' ⎕vfi   '£2£,3     ££££4.50'
 1 1 1 1 1 1 1 1  0 2 0 3 0 0 0 4.5  

Notice how blank remains "special"

It is probably wisest to do some prior cleanup, noting also...

      ⎕vfi '2 -3'
 1 0  2 0
      ⎕vfi '2 ¯3'
 1 1  2 ¯3

Which you may find mildly bizarre if we are taking data from outside the APL world.

Page created 13 April 2009.
Copyright © Dogon Research 2009.