This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Checking leftmost character of a list... Newbie...

Old posts that have not been replied to for several years.
Locked
K
Kingster

Checking leftmost character of a list... Newbie...

Post by Kingster »

Once again, I find myself stuck. And I am quite positive that for the TCL gurus floating around here that this will be quite easy...

Basically I want to evaluate a line from a list as I am outputting it...

Lines in the list will be something like:

0 15 (123ms) JohnnyRotten
1 24 (73ms) TomThumb

I want to look at the first character (either the 0 or 1) and then assign a color based on the number... Red for 0, Blue for 1... As I am pushing it via a putquick privmsg statement...

I know that the answer is probably quite obvious, and I have looked at the TCL docs... But I just can't get it figgered out...

I am guessing that it will be something like:

Code: Select all

foreach output_line $output_list {
  if {[string range $output_line 0 0]}==0{
    putquick "PRIVMSG $nick : \00304,99 $output_line"
  } elseif {[string range $output_line 0 0]}==1{
    putquick "PRIVMSG $nick : \00302,99 $output_line"
  } else {
    putquick "PRIVMSG $nick : \00301,99 $output_line"
  }
}
Am I on the right track?
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

Try "string index" for one.

Note your syntax of the if statment is wrong.

If uses the format
if {MatchExpr1} {
Body1
} elseif {MatchExpr2} {
Body2
} else {
Body3
}
There can be as many elseif statments within the if stament as you wish, however, if will only trigger/call the Body, of the first matching statment. Thus, you can't have more than 1 possitive match per IF.

All you matching must take place, within the MatchExpr part of the command.

If works, by evaluating staments. For if to work, it must evaluate to TRUE (any value above or below 0). It will not work, if evaluated to FALSE (any 0 value).

You reverse a value, so that TRUE becomes FALSE, and FALSE becomes TRUE, using the negate character !.

IE.
[string match "*d*" "hello"] == FALSE
![string match "*d*" "hello"] == TRUE

This first one can be spoken,
Run if "hello" has a "d" in it.
The second,
Run if "hello" doesn't have a "d" in it.
In your IF stament, your MatchExpr was running partaily outside the MatchExpr brackets.

It is as simple as moving a } past the ==0 and ==1
d
darko``
Op
Posts: 121
Joined: Sun Sep 08, 2002 5:33 pm
Location: Malta

Post by darko`` »

There is a neat solution for your problem:

.set blah 4
.putlog "Gimme \003${blah}colors!"

Try that, and you will get a coloured output based on the value of $blah.

Now, knowing that, it is obvious that you can avoid all those nasty if, else, elseif.

All you need to do is set some variable to the value of the first char and use it in the way i've shown. Of course, you will have to pay attention how you create database, so the first char will actually match the mirc color codes. That can be avoided too, but would involve creating tables and/or lists.
Ignorant and lazy people will save 30 minutes by chosing simple config file. Smart ones will save 3000 minutes of *everyone's* time by opting for complete config file.
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

I am guessing the above solution, is based ont he fact that he is using a DB produced by himself, or a program he has created.

Most DB's are beyond the users control, and as such, if-elseif-else is the only solution.
Locked