mIRC Home    About    Download    Register    News    Help

Print Thread
#261227 19/08/17 04:19 PM
Joined: Dec 2016
Posts: 18
A
apoio82 Offline OP
Pikka bird
OP Offline
Pikka bird
A
Joined: Dec 2016
Posts: 18
Good day I made a command where I find the first occurrence of a word in a txt and returns the entire line to a channel
On *: text: word:
Msg # $ read (c:\test.txt, nts, $ 2)
}
My doubt is how I would do to find the last occurrence of this word.
ex:
test.txt

Yellow ball
Blue ball

Which command would you use to find the word "ball" she being the second occurrence "Blue Ball"?

Thank You

Joined: Jan 2005
Posts: 192
Vogon poet
Offline
Vogon poet
Joined: Jan 2005
Posts: 192
Take a look at /filter


echo -a $signature
Joined: Jan 2004
Posts: 2,127
Hoopy frood
Offline
Hoopy frood
Joined: Jan 2004
Posts: 2,127
$read(c:\test.txt,ntw,*ball*) finds the first occurrence of the string 'ball' even if it's part of words like balloon or eyeball. If you want to find it only as a complete word, the problem with searching $read(c:\test.txt,ntw,* ball *) is that it won't find when ball is not preceded and followed by a space, as when it begins or ends the line. So you would be forced to do the first search like:

reading the line with:
var %line $read(c:\test.txt,ntw,*ball*)

then check to see if the line returns ball as a word token or was part of another word:
var %word $findtok(%line,ball,1,32)

if %line is blank, then the string 'ball' isn't in the file.
if %word is blank, then the string 'ball' was part of another word or was touching non-space delimiters like !.,? etc.
If you want to find 'ball' even if it touches commas, first change the punctuation to spaces before using findtok:

var %word $replace(%word,!,$chr(32),$chr(44),$chr(32),$chr(63),$chr(32),.,$chr(32))

If that line contained what you want to count as the 1st occurrence of 'ball', you increment your counter from 0 to 1.

$readn contains the line number last read using $read, so you can find the next line with 'ball' by starting to read at the next line:

var %line $read(c:\test.txt,ntw,*ball*, $calc(1+$readn) )

that last parameter is optional, and if not used it assumes start with line #1.

You then repeat the process of finding if that next line counts as 'ball', and if not keep repeating until you do find it or %line is blank because it reached the end of the file and found nothing.

If you want to find the Nth occurrence and N is a large number, you probably want to use /filter to grab all occurrences of *ball* to a @window to avoid N disk reads.

You can make your search much faster by doing some pre-processing of your text file to ensure that 'ball' is always preceded AND followed by a delimiter. Find a character that won't appear in your text file like the ^ symbol, then change all spaces into ^ and also make sure each line begins and ends with the ^ symbol. This way you can filter *^ball^* into a @custom window and know that line N of the @custom window will be the Nth occurrence of ball that you want.

Joined: Dec 2016
Posts: 18
A
apoio82 Offline OP
Pikka bird
OP Offline
Pikka bird
A
Joined: Dec 2016
Posts: 18
Thank you.
Just one more question if I wanted to show all occurrences of a word
For example, the word appears in 4 lines how do I do all lines are sent through msg.
Which command should I run?

Joined: Jan 2004
Posts: 2,127
Hoopy frood
Offline
Hoopy frood
Joined: Jan 2004
Posts: 2,127
You could either msg each line as you discover it, or else write them to a textfile then use /play to send to channel while you're doine. Unfortunately you can't use a @custom as the source of /play, but at least /play can filter your output through flood protection.


Link Copied to Clipboard