0

I am using this to find customer name in text file. Names are each on a separate line. I need to find exact name. If searching for Nick specifically it should find Nick only but my code will say found even if only Nickolson is in te list.

On*:text:*!Customer*:#: {
 if ($read(system\Customer.txt,$2)) { 
 .msg $chan $2 Customer found in list! | halt }
 else { .msg $chan 4 $2 Customer not found in list. | halt }
}

2 Answers2

0

You have to loop through every matching line and see if the line is an exact match

Something like this

On*:text:*!Custodsddmer*:#: {
  var %nick
  ; loop over all lines that contains nick
  while ($read(customer.txt, nw, *nick*, $calc($readn + 1))) {
    ; check if the line is an exact match
    if ($v1 == nick) {
      %nick = $v1
      ; stop the loop because a result is found
      break;
    }
  }
  if (%nick == $null) {
    .msg $chan 4 $2 Customer not found in list.
  }
  else{
  .msg $chan $2 Customer found in list!
}

You can find more here: https://en.wikichip.org/wiki/mirc/text_files#Iterating_Over_Matches

Denny
  • 1,766
  • 3
  • 17
  • 37
0

If you're looking for exact match in a new line separate list, then you can use the 'w' switch without using wildcard '*' character.

From mIRC documentation

$read(filename, [ntswrp], [matchtext], [N])

Scans the file info.txt for a line beginning with the word mirc and returns the text following the match value. //echo $read(help.txt, w, *help*)

Because we don't want the wildcard matching, but a exact match, we would use:

$read(customers.txt, w, Nick)

Complete Code:

ON *:TEXT:!Customer *:#: {
  var %foundInTheList = $read(system\Customer.txt, w, $2)
  if (%foundInTheList) {
    .msg # $2 Customer found in list!
  }
  else {
    .msg 4 # $2 Customer not found in list.
  }
}

Few remarks on Original code

Halting

halt should only use when you forcibly want to stop any future processing to take place. In most cases, you can avoid it, by writing you code flow in a way it will behave like that without explicitly using halting. It will also resolve new problems that may arise, in case you will want to add new code, but you will wonder why it isn't executing.. because of the darn now forgotten halt command. This will also improve you debugging, in the case it will not make you wonder on another flow exit, without you knowing.

Readability

if (..) {
.... }
else { .. }

When considering many lines of codes inside the first { } it will make it hard to notice the else (or elseif) because mIRC remote parser will put on the same identification as the else line also the line above it, which contains the closing } code. You should almost always few extra code in case of readability, especially which it costs new nothing!, as i remember new lines are free of charge.

So be sure the to have the rule of thump of every command in a new line. (that includes the closing bracket)

Matching Text

On*:text:*!Customer*:#: {

The above code has critical problem, and bug.

Critical: Will not work, because on*:text contains no space between on and *:text Bug: !Customer will match EVERYTHING-BEFORE!customerANDAFTER <NICK>, which is clearly not desired behavior. What you want is :!Customer *: will only match if the first word was !customer and you must enter at least another text, because I've used [SPACE]*.

Orel Eraki
  • 11,940
  • 3
  • 28
  • 36