mIRC Home    About    Download    Register    News    Help

Topic Options
#210090 - 04/03/09 03:09 PM Echo multiple lines from file
spermis Offline
Babel fish

Registered: 26/02/07
Posts: 91
Hello.
Could someone post a snippet, that would search a txt file for lines which contain part of the typed text?
For example the file is:
mp_101_kfs8
mp_ske_asf
mp_213_p0r
mp_101_kro
101_ksd_34m
mp_ksd_w0q
ksd_213_ras
mp_wk2_ksd

So when user types !search 101, it will show msg him in diferent lines like this:
1. Found mp_101_kfs8 in the file.
2. Found mp_101_kro in the file.
3. Found 101_ksd_34m in the file.

But if there are more than 3 results like, if user types !search ksd, it will show

1. Found 101_ksd_34m in the file.
2. Found mp_ksd_w0q in the file
3. Found ksd_213_ras in the file.
4. There are more than 3 results for "ksd" . Try being more correct.

Hope that you understood what i ment. Thanks.


Edited by spermis (04/03/09 03:10 PM)

Top
#210091 - 04/03/09 03:22 PM Re: Echo multiple lines from file [Re: spermis]
genius_at_work Offline
Hoopy frood

Registered: 08/10/05
Posts: 1741
/filter your file to a temp file to strip non-matching lines

Count the $lines remaining in the file

$read up to the first 3 lines

If there are more than 3 lines display your warning message

/remove the temp file

-genius_at_work

Top
#210092 - 04/03/09 03:31 PM Re: Echo multiple lines from file [Re: spermis]
Wims Offline
Planetary brain

Registered: 31/07/06
Posts: 3438
Loc: France
Code:
on *:text:!search *:#:{
filter yourtextfile.txt out.txt * $+ $2-*
var %a = 1, %b = $iif($filtered > 3,3,$v1)
while (%a <= %b) {
.timer 1 1 msg $chan Found $read(out.txt,%a) in the file.
if (%a == 3) && ($filtered >= 3) {
.timer 1 1 msg $chan There are more than 3 results for " $+ $2", try being more correct.
break
  }
inc %a
 }
.remove out.txt
}
Replace yourtextfile.txt with your file and try it, I have not tested, but should work.

Edit : the code is doing what genius_at_work said, but instead of strip non matching line, it put all matching line in a file.


Edited by Wims (04/03/09 03:34 PM)
_________________________
Looking for a good help channel about mIRC? Check #mircscripting @ irc.swiftirc.net

Top
#210107 - 04/03/09 09:03 PM Re: Echo multiple lines from file [Re: Wims]
spermis Offline
Babel fish

Registered: 26/02/07
Posts: 91
Originally Posted By: Wims
Code:
on *:text:!search *:#:{
filter yourtextfile.txt out.txt * $+ $2-*
var %a = 1, %b = $iif($filtered > 3,3,$v1)
while (%a <= %b) {
.timer 1 1 msg $chan Found $read(out.txt,%a) in the file.
if (%a == 3) && ($filtered >= 3) {
.timer 1 1 msg $chan There are more than 3 results for " $+ $2", try being more correct.
break
  }
inc %a
 }
.remove out.txt
}
Replace yourtextfile.txt with your file and try it, I have not tested, but should work.

Edit : the code is doing what genius_at_work said, but instead of strip non matching line, it put all matching line in a file.

Somehow nothing is happening at all frown

Top
#210110 - 04/03/09 10:17 PM Re: Echo multiple lines from file [Re: spermis]
Horstl Offline
Hoopy frood

Registered: 03/11/06
Posts: 1559
Loc: Germany
Try this modification of Wims' code. Instead of timers it will /filter to a 2nd tempfile to get the desired format and /play this file to the channel.

Make sure you put a valid source file.
Code:
on *:text:!search *:#:{

  ; #### Put line limit and sourcefile here. ####
  ; If the sourcefile isn't located in mIRCs main directory, you have to put the complete path and filename
  var %limit = 3, %file = test.txt

  ; filter lines that match the searchterm to 1st tempfile
  filter -ffc $qt(%file) !search.temp1 * $+ $2-*
  if ($filtered) {
    var %matches = $v1
    ; chop matches to %limit lines by filtering to 2nd tempfile and get the desired output format in this file
    filter -fknr $+(1-,%limit) !search.temp1 searchformat
    ; if the line limit was hit: add trailing line to 2nd tempfile
    if (%matches > %limit) { write !search.temp2 There are %matches results for $qt($2-) $+ , output limit is: %limit $+ . Try being more correct. }
    ; output content of 2nd tempfile to the channel with 750ms delay per line
    .play -q3m1 $chan !search.temp2 750
    .remove !search.temp2
  }
  else { .msg $chan Found no matches for $qt($2-) $+ . }
  .remove !search.temp1
}

alias -l searchformat {
  tokenize 32 $1-
  write $+(-l,$1) !search.temp2 $1 $+ . Found $2- in the file.
}

Edit: removed a redundant check for $2-

Top
#210111 - 04/03/09 10:43 PM Re: Echo multiple lines from file [Re: Horstl]
spermis Offline
Babel fish

Registered: 26/02/07
Posts: 91
Thanks. Working nice and smooth wink

Top
#210120 - 04/03/09 11:36 PM Re: Echo multiple lines from file [Re: spermis]
Wims Offline
Planetary brain

Registered: 31/07/06
Posts: 3438
Loc: France
Just in case, here the corrected code of mine :
Code:
on *:text:!search *:#:{
filter yourfilehere.txt out.txt * $+ $2-*
var %a = 1, %b = $iif($filtered > 3,3,$v1)
while (%a <= %b) {
.timer 1 %a  msg $chan Found $!( $qt($read(out.txt,n,%a)) ,0) in the file.
if (%a == 3) && ($filtered >= 3) {
.timer 1 4 msg $chan There are more than 3 results for " $+ $2", try being more correct.
   }
if (%a == 3) .remove out.txt
inc %a
  }
if (!$filtered) echo -a msg $chan No match found
}
_________________________
Looking for a good help channel about mIRC? Check #mircscripting @ irc.swiftirc.net

Top