|
Joined: Jun 2009
Posts: 9
Nutrimatic drinks dispenser
|
OP
Nutrimatic drinks dispenser
Joined: Jun 2009
Posts: 9 |
I'm trying to output all info from a text file (each line has 1 single word on it) in one message.
When I try $read(file.txt) I get a random line
When I try %lines $lines(file.txt) $read(file.txt,%lines) I get the last line in the file
When I try /play $chan file.txt I get the information I want, but in separate messages.
Say if my text file was: word1 word2 word3 word4 word5
I'd like the output to be something like: word1, word2, word3, word4, word5
Any suggestions would be helpful, thanks for your time.
|
|
|
|
Joined: Oct 2004
Posts: 8,330
Hoopy frood
|
Hoopy frood
Joined: Oct 2004
Posts: 8,330 |
$read(filename,N)
Replace N with the line number you want to use. Use a WHILE loop to read all lines and add them to a variable that you want to display. Keep in mind that too long of a line won't get completely sent.
There are other ways to do this as well, such as using the /fopen, $fread, etc commands, but that is more "complicated" if you're new to scripting. It really isn't that difficult, but $read is still easier, though slower for a large file.
Invision Support #Invision on irc.irchighway.net
|
|
|
|
Joined: Jul 2007
Posts: 1,129
Hoopy frood
|
Hoopy frood
Joined: Jul 2007
Posts: 1,129 |
alias output {
if ($isfile(file.txt)) {
echo # $regsubex($str(.,$lines(file.txt)),/(.)/g,$read(file.txt,\n) $+ $chr(44) $chr(32))
}
else { echo # file.txt is not found. Be sure it exists. }
} Usage: /output Replace file.txt with your actual file name.
|
|
|
|
Joined: Feb 2009
Posts: 133
Vogon poet
|
Vogon poet
Joined: Feb 2009
Posts: 133 |
I'd like the output to be something like: word1, word2, word3, word4, word5 there has 2 methods standar: alias wt {
var %x 1,%f $1
while ($read(%f,%x)) var %r %r $+ $chr(44) $v1,%x %x + 1
return $right(%r,-2)
} regex: alias wt return $regsubex($str(.,$lines($1)),/./g,$read(file.txt,\n) $+ $chr(44) $chr(32)) use: echo -a $wt(file)
WorldDMT
|
|
|
|
Joined: Nov 2006
Posts: 1,559
Hoopy frood
|
Hoopy frood
Joined: Nov 2006
Posts: 1,559 |
I wonder why the first example you come up with is that fancy $regsubex-"loop". It appears to me that Borg8401 is just starting to learn about loops in general... Why complicate the task with "advanced" but dispensable commands? *rollseyes* It's like throwing a way-out alias binaryexample {
bread words.txt 0 $file(words.txt).size &words
breplace &words 13 44 10 32
ECHO -a $bvar(&words,1,$bvar(&words,0)).text
} instead of a good ol' alias whileexample {
; a while-loop to $read all lines of "words.txt", adding the results to a variable "%string" each, separated by commas
var %n = 1, %string
while ($read(words.txt,n,%n) != $null) {
var %string = $addtok(%string,$v1,44)
inc %n
}
; for the desired output, replace all the commas in %string with "comma-and-space"
ECHO -a $replace(%string,$chr(44),$+($chr(44),$chr(32)))
}
Last edited by Horstl; 30/03/10 01:00 AM.
|
|
|
|
Joined: Jul 2007
Posts: 1,129
Hoopy frood
|
Hoopy frood
Joined: Jul 2007
Posts: 1,129 |
Well, I wasn't trying to be fancy but mainly showing a way of achieving Borg8401's end of the bargain. I know there are simpler ways to go about Borg8401 inquiry, but there are also people who only want something that works regardless of learning about it. Furthermore, if one does want to learn, he or she will find out and dissects the code to evaluate until they understand how a code works.
|
|
|
|
Joined: Jul 2007
Posts: 1,129
Hoopy frood
|
Hoopy frood
Joined: Jul 2007
Posts: 1,129 |
alias output {
if ($isfile(file.txt)) {
echo # $mid($regsubex($str(.,$lines(file.txt)),/./g,$read(file.txt,\n) $+ $chr(44) $chr(32)),1,-3)
}
else { echo # file.txt is not found. Be sure it exists. }
} Updated to remove the unneeded comma at the end of a word.
|
|
|
|
Joined: Feb 2009
Posts: 133
Vogon poet
|
Vogon poet
Joined: Feb 2009
Posts: 133 |
i wasnt test it edit for regex method me too alias wt {
var %t $1,%i $lines($1)
return $left($regsubex($str(.,%i),/./g,$read(%t,\n) $+ $chr(44) $chr(32)),-3)
} btw i like the binaryexample from horstl
WorldDMT
|
|
|
|
Joined: Jul 2007
Posts: 1,129
Hoopy frood
|
Hoopy frood
Joined: Jul 2007
Posts: 1,129 |
btw i like the binaryexample from horstl Yeah so do I. I was hoping he could be kind enough to show me another method by way of mirc's file handling and its commands.
|
|
|
|
|