|
Joined: Jan 2006
Posts: 9
Nutrimatic drinks dispenser
|
OP
Nutrimatic drinks dispenser
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.
|
|
|
|
Joined: Aug 2004
Posts: 7,252
Hoopy frood
|
Hoopy frood
Joined: Aug 2004
Posts: 7,252 |
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)
|
|
|
|
Joined: Sep 2005
Posts: 2,881
Hoopy frood
|
Hoopy frood
Joined: Sep 2005
Posts: 2,881 |
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>
|
|
|
|
Joined: Sep 2003
Posts: 4,230
Hoopy frood
|
Hoopy frood
Joined: Sep 2003
Posts: 4,230 |
;
;usage [color:blue]$filterfile2file4word(<file1>,<file2>,<word>)[/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.
|
|
|
|
Joined: Feb 2004
Posts: 2,019
Hoopy frood
|
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.
|
|
|
|
Joined: Sep 2003
Posts: 4,230
Hoopy frood
|
Hoopy frood
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.
|
|
|
|
Joined: Feb 2004
Posts: 2,019
Hoopy frood
|
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  Btw if you're interested you can use the following instead of { }: ; ! . / //
Gone.
|
|
|
|
Joined: Oct 2005
Posts: 1,741
Hoopy frood
|
Hoopy frood
Joined: Oct 2005
Posts: 1,741 |
Here is another version.. in case 3 versions wasn't enough.. :P
; 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) && ($isfile($2))) .remove $2
if ($fopen(fltr).fname) .fclose fltr
.fopen fltr $1
var %f, %r = 0
while ((!$fopen(fltr).eof) && (!$fopen(fltr).err)) {
%f = $fread(fltr)
var %c = 0, %cc = $numtok(%f,32)
while (%c < %cc) {
inc %c
if ($3 iswm $gettok(%f,%c,32)) { write $2 $v2 | inc %r }
}
}
.fclose fltr
return %r
}
-genius_at_work
|
|
|
|
Joined: Sep 2003
Posts: 4,230
Hoopy frood
|
Hoopy frood
Joined: Sep 2003
Posts: 4,230 |
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.
|
|
|
|
Joined: Feb 2004
Posts: 2,019
Hoopy frood
|
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.
|
|
|
|
Joined: Oct 2005
Posts: 1,741
Hoopy frood
|
Hoopy frood
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.
alias filterword {
if ((!$isfile($1)) || (!$isid)) return 0
if (($4) && ($isfile($2))) .remove $2
if ($fopen(fltr).fname) .fclose fltr
.fopen fltr $1
var %f, %r = 0
while ((!$fopen(fltr).eof) && (!$fopen(fltr).err)) {
%f = $fread(fltr)
var %c = 0, %cc = $numtok(%f,32)
while (%c < %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
|
|
|
|
Joined: Feb 2004
Posts: 2,019
Hoopy frood
|
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.
|
|
|
|
|