Filter

From Scriptwiki
Revision as of 13:55, 20 June 2007 by Cail (talk | contribs) (removed italic from 2 topics)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This command scans lines of text in a window or file and if any of them contain matchtext, they are written out to another window or file which you can then use.

/filter [-asdfkwxnpriocteubglLz] [n-n2] [c s] <infile | dialog id> <outfile | dialog id | alias> [alias] [matchtext]

Switches

The explanation of all switches at first:

Switch Meaning
a sorts filtered lines by calling the optional [alias]. The alias is passed two lines in $1 and $2, it must compare these and return -1, 0, or 1 to indicate relative sort order of these lines to each other. This can't be used with the t switch.
s makes the status window the infile.
d makes the single message window the infile.
f tells mIRC that the infile is actually a file.
k indicates that you have specified an <alias> as the output instead of a window name. The alias will be called with the result of each filtered line.
w tells mIRC that the infile is actually a window.
x excludes matching lines.
n prefixes lines with a line number.
p wraps the text output in a custom window.
r specifies the range of lines n to n2 for filtering.
i indicates that you have provided a [dialog id] custom dialog control as the input.
o indicates that you have provided a [dialog id] custom dialog control as the output.
c clears the output window/file before writing to it.
t sorts the output based on [c s], column C (number) using character S (ascii-code) as the columns separator. This can't be used with the a switch.
e specifies a descending sort.
u specifies a numeric sort.
b strips BURK codes when matching text.
g indicates that matchtext is a regular expression.
l filters from the side-listbox in the first window.
L filters from the side-listbox in the second window
z retains line colors when filtering between custom windows.


Note that you can filter blank lines by specifying $crlf for the matchtext.

The $filtered identifier is filled with the number of matches found, if any.


Some important facts about /filter:

  • If you specify an window as output, it has to be created first. mIRC will not create one. Contrary to that, mIRC will create files, if they do not exist.
  • If no matchtext is specified, all lines will be copied.
  • The specied windows / files must be in the correct order (-fw copies from file to window, -wf from window to file).

Example

For some of the examples, you will need:

  • a custom window. Open it with /window -e @filtertest
  • a text file. Make a blank one with /write -c filtertest.txt

Simple copying from files and windows

; This will copy your entire mIRC.ini in the @filtertest window.
filter -fw mIRC.ini @filtertest
; creates another window called @filtertest2, then copies only lines from @filtertest that begin with a open square bracket [.
window @filtertest2 | filter -ww @filtertest @filtertest2 [*


; Copies the entire versions.txt to your filtertest window, with clearing it before.
filter -cfw versions.txt @filtertest
; Copies all lines with change in it to @filtertest, with clearing it before again.
filter -cww @filtertest @filtertest *change*

Filtering lines

; This will filter every line in the Status Window that ends with "Unknown command." into a file called unknown.txt
filter -sf unknown.txt *Unknown command.
; Loads all lines from versions.txt into the Status Window
filter -fs versions.txt anytext
; Filter only lines 56 to 87 of versions.txt to the Status Window
filter -rfs 56-87 versions.txt anytext

Excluding text

; Filters all lines from #help.script to our @filtertest window but exclude lines that contain "you suck" 
filter -cxww #help.script @filtertest *you suck*
; Remove all lines from #help.script.log that start with -AnnoyingNoticer-.
; Note that ff tells mIRC that we want to filter the files, not the channel window.
filter -cxff #help.script.log #help.script.log -AnnoyingNoticer-*

Filtering Dialogs

; Save the contents of a listbox ID 3 in a dialog called "jukebox" to favorites.txt, making sure the text file is cleared before outputting to it
filter -cif jukebox 3 favorites.txt
; Example 28: Another similar song list, this time we don't want to save the songs that are in  C:\Music\Awful\
filter -xcif jukebox 3 favorites.txt *C:\Music\Awful\*

Filtering nicklist

; Filter the nicklist in the channel #help.script to a custom window called @#help.script_filter
filter -lw #help.script @#help.script_filter

Filtering with custom alias

; lets make a little alias comparing two numbers
alias sortnumber {
 return $iif($1 > $2, 1, $iif($1 == $2,0,-1))
}
; now let's fill a window with 1000 random numbers
var %i = 1
; lets loop from 1 to 1000
while (%i <= 1000) {
 ; and add a random number to the window
 aline @filtertest $rand(1,999999)
 inc %i
}
; lets make a second custom window
window @filtertest2
; now lets sort all the numbers and put them to the new window
filter -aww @filtertest @filtertest2 sortnumber

Filtering a scoreboard file

Lets say this is our scoreboard.txt file:

Albie 666 Ave IT :f
Dana 123 I'm the queen!
Q 690 I love my 69...0
God 34 I wish I was Albie
G 1 I wish i was dana :<
Fishbot 17 Fish go moo.

First is the nick, then their score, followed by their comment.

filter -tffceu 2 32 scoreboard.txt scoreboard.txt

The output now looks like this, with the highest score on top (Q with 690) and the lowest on the bottem (G with 1):

Q 690 I love my 69...0
Albie 666 Ave IT :f
Dana 123 I'm the queen!
God 34 I wish I was Albie
Fishbot 17 Fish go moo.
G 1 I wish i was dana :<

Explanation

  • The -t flag means that filter will sort using the options 2 32. The second token, delimited by ascii 32 (space)
  • The -ff flags mean the input and output is both scoreboard.txt, which is a filename.
  • The -c clears the output target before writing the output.
  • The -eu mean sort numerically (u) in descending order (e).


Example of multisort

Pretty much the same as "Filtering with custom alias", but loops through tokens of given strings and multisorts.

alias multisortnumber {
  var %x = 1
  while ($gettok($1,%x,32) == $gettok($2,%x,32)) && ($v1 != $null) inc %x
  if ($v1 == $v2) { return 0 }
  var %v1 = $v1, %v2 = $v2
  if (%v2 == $null) { return -1 }
  if (%v1 == $null) { return 1 }
  return $iif($calc(%v1 - %v2) > 0,1,-1)
}

alias filtermultisort {
  window @filtertest
  window @filtertest2

  var %i = 1
  while (%i <= 1000) {
    aline @filtertest $rand(170,180) $rand(170,180) $iif($r(0,1),$rand(170,180))
    inc %i
  }
  filter -aww @filtertest @filtertest2 multisortnumber
}