Determine if the argument is integer or string
7 posts
• Page 1 of 1
Determine if the argument is integer or string
I've encountered a following code to determine if the argument is an integer or a string:
This works! But how else it is possible to detect it in pure APL-way ?
- 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
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:
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
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
-
Morten|Dyalog - Posts: 453
- Joined: Tue Sep 09, 2008 3:52 pm
-
PGilbert - Posts: 436
- Joined: Sun Dec 13, 2009 8:46 pm
- Location: Montréal, Québec, Canada
Re: Determine if the argument is integer or string
I use ischar←{(⎕DR ⍵)∊80 160 320}
- michaelk
- Posts: 34
- Joined: Wed Feb 10, 2010 8:38 am
Re: Determine if the argument is integer or string
At present, numbers have an odd ⎕DR type, so this fork works:
isnum ← 2|⎕DRHowever, 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
Thanks for all answers!
- alexeyv
- Posts: 56
- Joined: Tue Nov 17, 2015 4:18 pm
Re: Determine if the argument is integer or string
isChar←{0=80|⎕DR ⍵}
-
norbertjurkiewicz84 - Posts: 62
- Joined: Mon Nov 01, 2010 7:26 pm
7 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 1 guest
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group