mIRC Home    About    Download    Register    News    Help

Topic Options
#175007 - 15/04/07 10:39 PM Checking case of first letter of a nick
RusselB Offline
Planetary brain

Registered: 03/08/04
Posts: 7252
Loc: Ontario, Canada
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.

Top
#175008 - 15/04/07 10:55 PM Re: Checking case of first letter of a nick [Re: RusselB]
hixxy Offline
Hoopy frood

Registered: 06/09/05
Posts: 2876
Code:
if ($left(<nick>,1) isupper) {  }
else {  }

Top
#175009 - 15/04/07 10:58 PM Re: Checking case of first letter of a nick [Re: RusselB]
jaytea Offline
Fjord artisan

Registered: 23/02/06
Posts: 546
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
_________________________
"The only excuse for making a useless script is that one admires it intensely" - Oscar Wilde

Top
#175010 - 15/04/07 11:05 PM Re: Checking case of first letter of a nick [Re: hixxy]
RusselB Offline
Planetary brain

Registered: 03/08/04
Posts: 7252
Loc: Ontario, Canada
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.

Top
#175011 - 15/04/07 11:10 PM Re: Checking case of first letter of a nick [Re: RusselB]
starbucks_mafia Offline
Hoopy frood

Registered: 09/12/02
Posts: 2962
Loc: Norwich, UK
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!


Edited by starbucks_mafia (16/04/07 03:58 AM)
_________________________
Spelling mistakes, grammatical errors, and stupid comments are intentional.

Top
#175012 - 15/04/07 11:13 PM Re: Checking case of first letter of a nick [Re: jaytea]
RusselB Offline
Planetary brain

Registered: 03/08/04
Posts: 7252
Loc: Ontario, Canada
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)

Top
#175013 - 15/04/07 11:22 PM Re: Checking case of first letter of a nick [Re: starbucks_mafia]
RusselB Offline
Planetary brain

Registered: 03/08/04
Posts: 7252
Loc: Ontario, Canada
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.

Top
#175018 - 16/04/07 01:39 AM Re: Checking case of first letter of a nick [Re: RusselB]
qwerty Offline
Hoopy frood

Registered: 07/01/03
Posts: 2523
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
}


_________________________
/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com

Top
#175020 - 16/04/07 02:54 AM Re: Checking case of first letter of a nick [Re: RusselB]
hixxy Offline
Hoopy frood

Registered: 06/09/05
Posts: 2876
Oops, I didn't read your post carefully enough. Sorry.

Top
#175022 - 16/04/07 03:19 AM Re: Checking case of first letter of a nick [Re: starbucks_mafia]
qwerty Offline
Hoopy frood

Registered: 07/01/03
Posts: 2523
These wouldn't match nicknames like "{2Cool}" or "_Bob_", as numbers and _ are neither letters nor \W.
_________________________
/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com

Top
#175024 - 16/04/07 04:04 AM Re: Checking case of first letter of a nick [Re: qwerty]
starbucks_mafia Offline
Hoopy frood

Registered: 09/12/02
Posts: 2962
Loc: Norwich, UK
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...
_________________________
Spelling mistakes, grammatical errors, and stupid comments are intentional.

Top
#175068 - 17/04/07 02:50 AM Re: Checking case of first letter of a nick [Re: RusselB]
CakerX Offline
Pikka bird

Registered: 22/10/04
Posts: 14
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


Edited by CakerX (17/04/07 02:51 AM)

Top
#175075 - 17/04/07 04:45 AM Re: Checking case of first letter of a nick [Re: CakerX]
RusselB Offline
Planetary brain

Registered: 03/08/04
Posts: 7252
Loc: Ontario, Canada
Like I said to hixxy, this checks the first character, not the first letter, per my original post

Top
#175079 - 17/04/07 05:34 AM Re: Checking case of first letter of a nick [Re: RusselB]
genius_at_work Offline
Hoopy frood

Registered: 08/10/05
Posts: 1741
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

Top
#175080 - 17/04/07 06:18 AM Re: Checking case of first letter of a nick [Re: genius_at_work]
RusselB Offline
Planetary brain

Registered: 03/08/04
Posts: 7252
Loc: Ontario, Canada
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.

Top
#175086 - 17/04/07 02:51 PM Re: Checking case of first letter of a nick [Re: RusselB]
genius_at_work Offline
Hoopy frood

Registered: 08/10/05
Posts: 1741
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


Top