mIRC Homepage
Posted By: AWEstun $read(filename.txt, s, test1) not working - 04/05/08 09:27 AM
I have a text file named filename.txt and in it is:

test1
test2
test3

When I type //echo $read(filename.txt, s, test1) nothing echos back to the screen. What am i doing wrong?

When I type //echo -a $lines(filename.txt) it echo's back the correct number of lines in the file.
You aren't specifying where the echo should go
Quote:
/echo [color] [-cdeghiNtsaqlbfnmr] [color name] [#channel|[=]nick] <text>

Prints text in the specified window using the specified color (0 to 15).

/echo 3 #mIRC Testing

would print "Testing" in the color green in channel window #mIRC, assuming it's already open.

If a channel/nickname isn't specified, the -s switch echoes to the status window, the -d switch echoes to the single message window, and the -a switch echoes to the currently active window.


Try
Code:
//echo -a $read(filename.txt,s,test1)
Thanks. I can't get the s switch to work, but the w switch with a * on the end of the search text did. For example:

//echo -a read(filename.txt,w,test1*)

This echos back: Test1

So why doesn't the s switch work?
Posted By: 5618 Re: $read(filename.txt, s, test1) not working - 04/05/08 10:10 AM
Read the help file:
Quote:
//echo $read(info.txt, s, mirc)

Scans the file info.txt for a line beginning with the word mirc and returns the text following the match value.

It's not returning anything because the is nothing *after* test1. If you make the line read "test1 hello" then your $read will return "hello".
Ah, that makes sense! I was reading the mirc help file, but i wasn't sure if it'd return what it found and the test after or just the text after what it found. Thanks for clearing that up.
How do I add a * to the end of a string? I.E. $4 $+ *

I tried

if $read(filename.txt,w,$4 $+ * == $null ) { write filename.txt $4 }

but i dont think that is working.

Basically I'm trying to search filename.txt for a string and if that string is not found, add it.
Posted By: qwerty Re: $read(filename.txt, s, test1) not working - 04/05/08 11:10 AM
You just have a misplaced closing bracket. Change

if $read(filename.txt,w,$4 $+ * == $null ) { write filename.txt $4 }

to

if $read(filename.txt,w,$4 $+ *) == $null { write filename.txt $4 }
Heh, yeah I just noticed that too.

Is there any kind of program that will resort a text file by the first letter of the line?
Posted By: qwerty Re: $read(filename.txt, s, test1) not working - 04/05/08 11:26 AM
/filter -ffca file.txt file.txt

will sort the file alphabetically, although not just by first letter (ie it will sort by first letter, but the words that have the same first letter will be sorted by second letter etc).

For more sorting options, you'd need to use the -t switch (/help /filter).
Thanks.

When I use $read to seach, it return the first match, but how can I get it to show all matches?
Posted By: Wims Re: $read(filename.txt, s, test1) not working - 04/05/08 01:49 PM
Again, with /filter, look in the help file for this command :
Code:
//filter in.txt out.txt *text*
Here out.txt will contain all line of in.txt that match *text*.
Originally Posted By: Wims
Again, with /filter, look in the help file for this command :
Code:
//filter in.txt out.txt *text*
Here out.txt will contain all line of in.txt that match *text*.


Thanks, but that's not what I was looking for. I removed the duplicates by using an external program.
How do I search if there are more than one match in the file? Let's say I'm searching for 'apple' and the text file has this:

orange
applepie
appleshake
banana
applesmoothie

if I do a $read(filename.txt,w,apple*) that will only show me 'applepie'. So how do I get it to show me the other matches too, like 'appleshake' and 'applesmoothie'?
Posted By: 5618 Re: $read(filename.txt, s, test1) not working - 04/05/08 05:10 PM
Like Wims said, you can use /filter and then $read the output .txt

Alternatively, you could do a dirty method like...
Code:
alias {
  var %line = 1
  while ($read(filename.txt,w,apple*,%line)) {
    echo -a $read(filename.txt,w,apple*,%line)
    var %line = $calc($readn +1)
  }
}

That sets the line to start the next wildcard match from to the one after the previous matched line ($readn).
You should use $v1 within the loop instead of re-searching the file otherwise you're doing double the necessary work.
Posted By: 5618 Re: $read(filename.txt, s, test1) not working - 04/05/08 06:00 PM
Originally Posted By: starbucks_mafia
You should use $v1 within the loop instead of re-searching the file otherwise you're doing double the necessary work.

Yes, you are completely right. It's so obvious yet easy to forget.
So my initial echo should be changed to "echo -a $v1".
Thanks, this was exactly what I'm looking for.
Posted By: RoCk Re: $read(filename.txt, s, test1) not working - 05/05/08 02:06 AM

Yes. The result of the $read in the while loop is already stored in $v1 so echoing the result using the $read identifier again is totally unnecessary and as starbucks_mafia said, it would be searching the file again when the result is already available in $v1.

/help $v1
© mIRC Discussion Forums