mIRC Home    About    Download    Register    News    Help

Print Thread
#219795 29/03/10 04:04 PM
Joined: Jun 2009
Posts: 9
B
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
B
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.

Borg8401 #219797 29/03/10 05:21 PM
Joined: Oct 2004
Posts: 8,330
Hoopy frood
Offline
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
Borg8401 #219800 29/03/10 06:23 PM
Joined: Jul 2007
Posts: 1,129
T
Hoopy frood
Offline
Hoopy frood
T
Joined: Jul 2007
Posts: 1,129
Code:
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.

Borg8401 #219801 29/03/10 07:05 PM
Joined: Feb 2009
Posts: 133
C
Vogon poet
Offline
Vogon poet
C
Joined: Feb 2009
Posts: 133
Originally Posted By: Borg8401
I'd like the output to be something like:
word1, word2, word3, word4, word5

there has 2 methods
standar:
Code:
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:
Code:
alias wt return $regsubex($str(.,$lines($1)),/./g,$read(file.txt,\n) $+ $chr(44) $chr(32))


use: echo -a $wt(file)


WorldDMT
Tomao #219813 30/03/10 12:57 AM
Joined: Nov 2006
Posts: 1,559
H
Hoopy frood
Offline
Hoopy frood
H
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
Code:
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'
Code:
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.
Horstl #219815 30/03/10 01:26 AM
Joined: Jul 2007
Posts: 1,129
T
Hoopy frood
Offline
Hoopy frood
T
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.

Borg8401 #219824 30/03/10 08:51 AM
Joined: Jul 2007
Posts: 1,129
T
Hoopy frood
Offline
Hoopy frood
T
Joined: Jul 2007
Posts: 1,129
Code:
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.

Tomao #219827 30/03/10 01:08 PM
Joined: Feb 2009
Posts: 133
C
Vogon poet
Offline
Vogon poet
C
Joined: Feb 2009
Posts: 133
i wasnt test it blush edit for regex method me too laugh

Code:
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
chacha #219833 30/03/10 04:31 PM
Joined: Jul 2007
Posts: 1,129
T
Hoopy frood
Offline
Hoopy frood
T
Joined: Jul 2007
Posts: 1,129
Originally Posted By: chacha
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.


Link Copied to Clipboard