character checking

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

character checking

Maguin, Eugene

I’m wanting to check whether a character is upper case vs anything else. I wrote something that I’m ‘sure’ I’ve used in the past and even ‘surer’ that it worked as desired. Now it doesn’t; so I need a replacement. This is the whole chunk of code. I expecting that if a=’m’, the if statement will fail; but if a=’M’, the test is true and groupauthor is 0.

 

string a(a1).

compute #i=char.length(author).

compute a=char.substr(author,#i,1).

compute groupauthor=1.

if (a ge 'A' and a le 'Z') groupauthor=0.

format groupauthor(f1.0).

execute.

 

Thanks, Gene Maguin

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: character checking

Jon Peck
If you know that the last character is alphabetic (not a number, blank, punctuation etc), you can just do this for a variable named s.
compute isupper = char.substr(s, char.length(s),1) eq upcase(char.substr(s,char.length(s),1)).

If you want a bulletproof method that handles anything, you can do this.
spssinc trans result = isupper type=4
/formula "unicodedata.category(s[-1].rstrip())".

If it is an uppercase character, the result will be "Lu"  (Unicode upper case letter).

The problem with the code above might be that you are not in Unicode mode, so trailing blanks are not stripped but you were before.



On Tue, Mar 31, 2020 at 7:01 AM Maguin, Eugene <[hidden email]> wrote:

I’m wanting to check whether a character is upper case vs anything else. I wrote something that I’m ‘sure’ I’ve used in the past and even ‘surer’ that it worked as desired. Now it doesn’t; so I need a replacement. This is the whole chunk of code. I expecting that if a=’m’, the if statement will fail; but if a=’M’, the test is true and groupauthor is 0.

 

string a(a1).

compute #i=char.length(author).

compute a=char.substr(author,#i,1).

compute groupauthor=1.

if (a ge 'A' and a le 'Z') groupauthor=0.

format groupauthor(f1.0).

execute.

 

Thanks, Gene Maguin

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD


--
Jon K Peck
[hidden email]

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: character checking

Maguin, Eugene

I can’t guarantee that the last character is only alpha. There definitely are periods and a left paren.

As to Unicode. I looked at the edit ->options and Character encoding is set to Unicode and not to local system.

 

Re: your ps. One does have an accent character but it may be the only one  and I can find that because it ‘should’ give a zero in the test.

 

 

From: Jon Peck [mailto:[hidden email]]
Sent: Tuesday, March 31, 2020 10:10 AM
To: Maguin, Eugene <[hidden email]>
Cc: SPSS List <[hidden email]>
Subject: Re: [SPSSX-L] character checking

 

If you know that the last character is alphabetic (not a number, blank, punctuation etc), you can just do this for a variable named s.

compute isupper = char.substr(s, char.length(s),1) eq upcase(char.substr(s,char.length(s),1)).

 

If you want a bulletproof method that handles anything, you can do this.

spssinc trans result = isupper type=4
/formula "unicodedata.category(s[-1].rstrip())".

 

If it is an uppercase character, the result will be "Lu"  (Unicode upper case letter).

 

The problem with the code above might be that you are not in Unicode mode, so trailing blanks are not stripped but you were before.

 

 

 

On Tue, Mar 31, 2020 at 7:01 AM Maguin, Eugene <[hidden email]> wrote:

I’m wanting to check whether a character is upper case vs anything else. I wrote something that I’m ‘sure’ I’ve used in the past and even ‘surer’ that it worked as desired. Now it doesn’t; so I need a replacement. This is the whole chunk of code. I expecting that if a=’m’, the if statement will fail; but if a=’M’, the test is true and groupauthor is 0.

 

string a(a1).

compute #i=char.length(author).

compute a=char.substr(author,#i,1).

compute groupauthor=1.

if (a ge 'A' and a le 'Z') groupauthor=0.

format groupauthor(f1.0).

execute.

 

Thanks, Gene Maguin

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD


 

--

Jon K Peck
[hidden email]

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD