mIRC Home    About    Download    Register    News    Help

Print Thread
Filter words from a textfile #141374 08/02/06 10:07 PM
Joined: Jan 2006
Posts: 9
C
cheatvdb Offline OP
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
C
Joined: Jan 2006
Posts: 9
Hello, I can filter words (name*) from a text file and write them in another text file but then the whole line is displayed when you use the /filter -ff command. I just want this:

file1.txt:
I want to get the word dog out of this line and put in another line
this doggie word should be copied to and also doghouse

file2.txt:
dog
doggie
doghouse

simple I thought, but nothing found yet, even with $read on a wildcard...

any help is appreciated

Last edited by cheatvdb; 08/02/06 10:09 PM.
Re: Filter words from a textfile #141375 08/02/06 11:52 PM
Joined: Aug 2004
Posts: 7,252
R
RusselB Offline
Hoopy frood
Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
Code:
 on *:text:&:#:{
  var %a = 1
  while $read(word.txt,ntw,$+($1,*),%a) {
    .write word2.txt $read(word.txt,ntw,$+($1,*),%a)
    var %a = $calc(1 + $readn)
  }
}
 


That will take a one word entry (entered by someone other than the person/bot that's running the code), search the word.txt file for each entry that starts with that word, then writes that entry to the new file (I called it word2.txt)

Re: Filter words from a textfile #141376 09/02/06 12:22 AM
Joined: Sep 2005
Posts: 2,876
H
hixxy Offline
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,876
Code:
alias filterfile {
  tokenize 34 $1-
  set -u %outfile $+(",$3,")
  filter -fk $+(",$1,") _filterfile $4 $+ *
}
alias _filterfile {
  if (%outfile != $null) write $+(",%outfile,") $gettok($1-,1,32)
}


/filterfile "<path to file one>" "<path to file two>" <wildcard>

Re: Filter words from a textfile #141377 09/02/06 12:30 AM
Joined: Sep 2003
Posts: 4,230
D
DaveC Offline
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
Code:
;
;usage [color:blue]$filterfile2file4word(&lt;file1&gt;,&lt;file2&gt;,&lt;word&gt;)[/color]
;return 0 to X,  X being the number of words ADDED to file2
;
alias filterfile2file4word {
  set %filterfile2file4word.$2 $2
  set %filterfile2file4word.$3 $3
  set %filterfile2file4word.return 0
  filter -fk $+(",$1,") filterfile2file4word.filter.alias $+(*,$3,*)
  unset %filterfile2file4word.$2
  unset %filterfile2file4word.$3
  if (%filterfile2file4word.return) { }
  unset %filterfile2file4word.return
  return $v1
}
alias filterfile2file4word.filter.alias {
  tokenize 32 $1-
  while ($0) {
    if (%filterfile2file4word.$3 iswm $1) { write $+(",%filterfile2file4word.$2,") $1 | inc %filterfile2file4word.return }
    tokenize 32 $2-
  }
}


* mirc 6.16 required *
* no error checking is performed on the filenames or your access right to them, thats your fault if u put grabage in you get garbage out.
* <word> is any word with or without wildcards.

Re: Filter words from a textfile #141378 09/02/06 01:28 PM
Joined: Feb 2004
Posts: 2,019
FiberOPtics Offline
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2004
Posts: 2,019
It would be much nicer to use set -u then you don't need to unset those global vars. Also, that empty if statement serves no purpose.


Gone.
Re: Filter words from a textfile #141379 09/02/06 07:12 PM
Joined: Sep 2003
Posts: 4,230
D
DaveC Offline
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
To true about the -u i wasnt really thinking overly hard on it, as in i just added bits as i ran into the need.
Your statment on the IF should have read "Also, that empty if statement would then serve no purpose."
The IF is needed in the construct right now, see 2 lines down from it, im destroying the var im using as the retuirn value so had to store its value somewhere.

Re: Filter words from a textfile #141380 09/02/06 07:24 PM
Joined: Feb 2004
Posts: 2,019
FiberOPtics Offline
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2004
Posts: 2,019
Even though I didn't see that you indeed used it to fill $v1, you could still just have done:

.timer 1 0 unset %var
return %var

Although, thinking about it now, I guess this boils down to personal preference, but I still think it's silly grin

Btw if you're interested you can use the following instead of { }: ; ! . / //


Gone.
Re: Filter words from a textfile #141381 09/02/06 08:54 PM
Joined: Oct 2005
Posts: 1,741
G
genius_at_work Offline
Hoopy frood
Offline
Hoopy frood
G
Joined: Oct 2005
Posts: 1,741
Here is another version.. in case 3 versions wasn't enough.. :P

Code:
; Usage: $filterword(infile,outfile,search,overwrite)
; - infile = text file to search
; - outfile = text file for results
; - search = word (may include wildcards) to search for
; - overwrite = overwrite outfile if it exists
; * returns number of matches found

alias filterword {
  tokenize 32 $+(",$1,") $+(",$2,") $3-
  if (($4) &amp;&amp; ($isfile($2))) .remove $2
  if ($fopen(fltr).fname) .fclose fltr
  .fopen fltr $1
  var %f, %r = 0
  while ((!$fopen(fltr).eof) &amp;&amp; (!$fopen(fltr).err)) {
    %f = $fread(fltr)
    var %c = 0, %cc = $numtok(%f,32)
    while (%c &lt; %cc) {
      inc %c
      if ($3 iswm $gettok(%f,%c,32)) { write $2 $v2 | inc %r }
    }
  }
  .fclose fltr
  return %r
}


-genius_at_work

Re: Filter words from a textfile #141382 09/02/06 09:22 PM
Joined: Sep 2003
Posts: 4,230
D
DaveC Offline
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
Quote:
tokenize 32 $+(",$1,") $+(",$2,") $3-


you cant do that, if $1 was say c:\temp folder\file1.txt the tokenizing well result in
$1 = "c:\temp
$2 = folder\file1.txt"

Ignoring that looked like nice code.

Re: Filter words from a textfile #141383 09/02/06 10:06 PM
Joined: Feb 2004
Posts: 2,019
FiberOPtics Offline
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2004
Posts: 2,019
I would do a check for $ferr after opening the file, to be certain it was opened successfully.


Gone.
Re: Filter words from a textfile #141384 09/02/06 11:11 PM
Joined: Oct 2005
Posts: 1,741
G
genius_at_work Offline
Hoopy frood
Offline
Hoopy frood
G
Joined: Oct 2005
Posts: 1,741
That was a dumb mistake on my part. I decided to remove the " part completely, so it is more like other commands where you include "s if the filename has spaces.

Code:
alias filterword {
  if ((!$isfile($1)) || (!$isid)) return 0
  if (($4) &amp;&amp; ($isfile($2))) .remove $2
  if ($fopen(fltr).fname) .fclose fltr
  .fopen fltr $1
  var %f, %r = 0
  while ((!$fopen(fltr).eof) &amp;&amp; (!$fopen(fltr).err)) {
    %f = $fread(fltr)
    var %c = 0, %cc = $numtok(%f,32)
    while (%c &lt; %cc) {
      inc %c
      if ($3 iswm $gettok(%f,%c,32)) { write $2 $v2 | inc %r }
    }
  }
  if ($fopen(fltr).fname) .fclose fltr
  return %r
}


---------------

@FOp, is $fopen(name).err the same as $ferr ? If so, then the error checking for the /fopen is done in the while loop.

-genius_at_work

Re: Filter words from a textfile #141385 09/02/06 11:14 PM
Joined: Feb 2004
Posts: 2,019
FiberOPtics Offline
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2004
Posts: 2,019
No they're not the same, although generally you'll be fine to use $feof and $ferr, the difference is that these identifiers are global, whereas $fopen(name).err/.eof are local to this file only. Though in a script, these identifiers will always represent the values of the file accessed in the script.

Btw the problem isn't the while loop, I know it wouldn't error there, but the fclose would give an error if it wasn't open, therefore you must check $ferr right after /fopen.

In your new version it is better because you use $fopen(name) before closing, but it could still be improved a bit. Observe:

//fopen blabla lol | echo -a > $fopen(blabla) vs $ferr

Even though there was an error to open the file (* fopen unable to open 'blabla' (D:\mIRC\lol)) $fopen(name) returns the name. Note that you will however be able to close it just fine since mIRC actually initiated a "file" object, despite the fact it was unable to open the specific file, so for your script checking $fopen(name) like you do now is sufficient.


Gone.