mIRC Homepage
Posted By: RusselB Checking case of first letter of a nick - 15/04/07 09:39 PM
I thought I had asked something like this before, but the search only returned the last 100 entries made by me, inspite of the fact that I had specified a 5 year period, and the search only brought me back to the beginning of 2007.

In any case, I'm looking for something that will allow my bot to recognize if the first letter (not the first character) in a nick is upper or lower case.
Posted By: hixxy Re: Checking case of first letter of a nick - 15/04/07 09:55 PM
Code:
if ($left(<nick>,1) isupper) {  }
else {  }
Posted By: jaytea Re: Checking case of first letter of a nick - 15/04/07 09:58 PM
couple of methods:

Code:
  var %x = 1
  while ($$mid($nick,%x,1) !isletter) inc %x
  if ($v1 isupper) {
    ; commands
  }


the extra $ in $$mid() makes it /halt if the nick contains no letters. you could also be sneaky and use if ($v1 < a) which checks it's upper case (lexicographic comparison) :P

or the regex method:

Code:
if ($regex($nick,/^[^a-zA-Z]*[A-Z]/)) {
  ; commands
}


which also checks it's uppercase
Posted By: RusselB Re: Checking case of first letter of a nick - 15/04/07 10:05 PM
That would check the first character, which may be what I want, but some people use nicks that start with characters other than a letter, which is why I specified that I wanted to check the first letter, not the first character.
Here's a couple of regex-based methods.

Code:
alias startsupper return $iif($regex($1,/^[\W\d_]*(?=[^\d_])(\w)/) && $regml(1) isupper, $true, $false)
alias startsupper2 return $regex($1,/^[^a-zA-Z]*[A-Z]/)


Although $startsupper() is longer it should in theory be future-proof for if/when mIRC ever supports Unicode in isupper (by the looks of things it doesn't yet although I'm only using 6.2).

Edit: Fixed digit/underscore issue. Thanks qwerty!
Posted By: RusselB Re: Checking case of first letter of a nick - 15/04/07 10:13 PM
Thanks Jaytea: Since I want different commands performed depending on if the first letter of the person's nick is capitalized or not (and the fact that I don't understand the regex), it looks like the first code you provided me with would be best.

If you disagree, and think the regex would be better, I would appreciate an explanation of the regex. (actually, I wouldn't mind the explanation either way)
Posted By: RusselB Re: Checking case of first letter of a nick - 15/04/07 10:22 PM
Thanks starbucks_mafia, however, as I stated in my reply to Jaytea, I don't understand regex and I prefer to use code that I understand, even if it's longer.
Posted By: qwerty Re: Checking case of first letter of a nick - 16/04/07 12:39 AM
Since the set of allowed chars in nicknames is not that big (all servers I've seen so far conform to RFC 2812, regarding the allowed chars; only lengths of nicks vary), yet another alternative would be:
Code:
if ($asc($remove($nick,-,[,],\,`,_,^,$chr(123),|,$chr(125),0,1,2,3,4,5,6,7,8,9)) isnum 65-90) {
  echo -a first letter is uppercase
}


Posted By: hixxy Re: Checking case of first letter of a nick - 16/04/07 01:54 AM
Oops, I didn't read your post carefully enough. Sorry.
Posted By: qwerty Re: Checking case of first letter of a nick - 16/04/07 02:19 AM
These wouldn't match nicknames like "{2Cool}" or "_Bob_", as numbers and _ are neither letters nor \W.
Yeah, messed that up didn't I? I edited the original post so nobody has to see my bugged code. Instead they can see working code that's ugly as sin with that added lookahead assertion, but I can't think of any other way that would support unicode.

Not that unicode support is probably even needed since this is for nicks...
And not that isupper even supports unicode yet, if it ever will...
Posted By: CakerX Re: Checking case of first letter of a nick - 17/04/07 01:50 AM
of course:
Code:
If ( $asc( $left( $nick ,1)) <= 90 ) { echo character is uppercase}
else { echo character is lowercase }


edit: this asumes this is working from an event which sets $nick
Posted By: RusselB Re: Checking case of first letter of a nick - 17/04/07 03:45 AM
Like I said to hixxy, this checks the first character, not the first letter, per my original post
I know you said you'd like to stay away from regex solutions, but in this case, they are the most elegant. Here are two different regex solutions that are relatively easy to understand.

First is this identifier:
Code:
alias firstletter { noop $regex($1,/([a-z])/i) | return $regml(1) }


This code simply returns the first letter from $1, whether it is upper or lowercase. You can do whatever you want with it. In this case you could do something like this:

... if ($firstletter($nick) isupper) echo -a $v1 is UPPER

An explanation of the regex:

/ = beginning of regex
( = beginning of capture group 1
[a-z] = any character from a-z (not case sensitive, see i below)
) = end of capture group
/ = end of regex
i = regex is case insensitive

------------------------------------------

The second identifier:
Code:
alias isfirstupper return $regex($1,/^[^a-z]*[A-Z]/)


This code returns 1 if the first letter is uppercase and 0 if the first letter is not uppercase. This code would be used like this:

... if ($isfirstupper($nick)) echo -a First character is UPPER

This regex is slightly more complex and more difficult to understand.

/ = beginning of regex
^ = beginning of string
[^a-z]* = 0 or more of any character that isn't a-z (IS case sensitive)
[A-Z] = any character from A-Z (IS case sensitive)
/ = end of regex

Basically, from the beginning of the string, if there are 0 or more characters except a-z (CS), followed by A-Z. The rest of the string is abandoned once the first A-Z is found.

-------------------------------------------

You want to stay away from regex, but I think the first regex example is simple enough that you may want to bend that rule a bit. ;P

-genius_at_work
Posted By: RusselB Re: Checking case of first letter of a nick - 17/04/07 05:18 AM
The only reason I wanted to stay away from regex, is because I don't understand it, however, you provided an explanation of the format for the regex code, and when I get a chance I'll look it over to make sure I understand it.

I wouldn't expect anyone to use a code that they didn't understand, and I won't use one that I don't understand, but I am willing to learn.
What you are asking for can be done with a simple while loop, but the first regex I gave does the exact same thing, just in the background where you can't see it.

Code:
alias firstletter1 {
  noop $regex($1,/([a-z])/i)
  return $regml(1)
}

alias firstletter2 { 
  var %i = 0, %ii = $len($1)
  while (%i < %ii) {
    inc %i
    if ($mid($1,%i,1) isalpha) return $v1
  }
}


Both of those codes should return the same thing.

-genius_at_work

© mIRC Discussion Forums