Determine if the argument is integer or string

General APL language issues

Determine if the argument is integer or string

Postby alexeyv on Wed Dec 21, 2016 7:11 pm

I've encountered a following code to determine if the argument is an integer or a string:
Code: Select all
{' '≡↑0⍴⍵}

This works! But how else it is possible to detect it in pure APL-way ?
alexeyv
 
Posts: 56
Joined: Tue Nov 17, 2015 4:18 pm

Re: Determine if the argument is integer or string

Postby Morten|Dyalog on Wed Dec 21, 2016 8:39 pm

Strictly speaking, that expression tells you whether the argument is character or not, it doesn't tell you what kind of number (boolean, integer, float, complex) it might be. On the other hand, I would say that it is "pure APL", since it only uses primitives.

It might be more direct to use ⎕DR, which returns a number which is the data type + 10×the bit size of the element:

Code: Select all
      {(⍕¨⍵),⍪⎕DR¨⍵}'A' '⍋' (0 1) 127 1000 40000 1.1 1J2
 A        80
 ⍋       160
 0 1      11
 127      83
 1000    163
 40000   323
 1.1     645
 1J2    1289

Type 0 is character, 3 is integer (but boolean arrays can have type 1). You can read all about ⎕DR on the online help pages
User avatar
Morten|Dyalog
 
Posts: 394
Joined: Tue Sep 09, 2008 3:52 pm

Re: Determine if the argument is integer or string

Postby PGilbert on Thu Dec 22, 2016 12:26 pm

In case this is helping:

Code: Select all
isString←{0 2∊⍨10|⎕DR ⍵}
User avatar
PGilbert
 
Posts: 401
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada

Re: Determine if the argument is integer or string

Postby michaelk on Fri Dec 23, 2016 3:42 am

I use ischar←{(⎕DR ⍵)∊80 160 320}
michaelk
 
Posts: 22
Joined: Wed Feb 10, 2010 8:38 am

Re: Determine if the argument is integer or string

Postby JohnS|Dyalog on Fri Dec 23, 2016 6:41 am

At present, numbers have an odd ⎕DR type, so this fork works:
      isnum ← 2|⎕DR
However, we might be forced to break this rule in future when allocating the remaining codes: 4, 6 and 8.
JohnS|Dyalog
 

Re: Determine if the argument is integer or string

Postby alexeyv on Sun Dec 25, 2016 6:16 pm

Thanks for all answers!
alexeyv
 
Posts: 56
Joined: Tue Nov 17, 2015 4:18 pm

Re: Determine if the argument is integer or string

Postby norbertjurkiewicz84 on Thu Jan 19, 2017 6:04 pm

isChar←{0=80|⎕DR ⍵}
User avatar
norbertjurkiewicz84
 
Posts: 60
Joined: Mon Nov 01, 2010 7:26 pm


Return to Language

Who is online

Users browsing this forum: No registered users and 1 guest