mIRC Home    About    Download    Register    News    Help

Print Thread
Changing a line in a txtfile #137230 10/12/05 02:08 PM
Joined: Mar 2005
Posts: 39
B
bapplander Offline OP
Ameglian cow
OP Offline
Ameglian cow
B
Joined: Mar 2005
Posts: 39
I have a txtfile with data stored as data1 data2 data3.
Is is possible to do a !change data3 newdata2?

Meaning, !change will search the file for data3 and replace data2 with newdata2 ?

Re: Changing a line in a txtfile #137231 10/12/05 02:52 PM
Joined: Sep 2003
Posts: 4,230
D
DaveC Offline
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
Is this ment to be like a text trigger? well heres both a triggered one or you can type it, you might need to localise access to the trigger I dont know.

on *:TEXT:!change & &:*:{ change $2 $3 }
alias change { if ($read(txtfile.txt,ntw,& & $1)) { write $+(-l,$readn) txtfile.txt $puttok($v1,$2,2,32) } }

Re: Changing a line in a txtfile #137232 10/12/05 04:14 PM
Joined: Mar 2005
Posts: 39
B
bapplander Offline OP
Ameglian cow
OP Offline
Ameglian cow
B
Joined: Mar 2005
Posts: 39
hm..it seems to only delete the line, not replacing it with something new.. shocked

Re: Changing a line in a txtfile #137233 10/12/05 05:26 PM
Joined: Aug 2004
Posts: 7,252
R
RusselB Offline
Hoopy frood
Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
Code:
 on *:text:!change & &:#:{
  var %a = 1
  while %a <= $lines(file.txt) {
    var %line = $read(file.txt,ntw,$2,%a)
    var %line = $reptok(%line,$gettok(%line,2,32),$3,1,32)
    .write -l $+ $readn file.txt %line
    var %a = $readn
  }
}
 

Re: Changing a line in a txtfile #137234 10/12/05 06:01 PM
Joined: Dec 2002
Posts: 1,245
M
MikeChat Offline
Hoopy frood
Offline
Hoopy frood
M
Joined: Dec 2002
Posts: 1,245
you have a comparison inside the while that every time it loops must be re-evaluated, very inefficient to do it that way
really except %i I dont use short var names like %a or %b as you stand a good chance of having a conflict.
you didnt use ( ) in the while loop, though you might be able to do that now in your version of mIRC there is no quarenty that you can shortcut that later or in other versions, so its probably a good habit to use the form shown in the help files.
I changed the %a = $readn in the script and that may be wrong, I didn't test this in any way.
Code:
on *:text:!change & &:#:{
  var %a = 1
  var %b = $lines(file.txt)
  while (%a <= %b) {
    var %line = $read(file.txt,ntw,$2,%a)
    var %line = $reptok(%line,$gettok(%line,2,32),$3,1,32)
    .write -l $+ $readn file.txt %line
    inc %a
  }
}

Re: Changing a line in a txtfile #137235 10/12/05 06:21 PM
Joined: Aug 2004
Posts: 7,252
R
RusselB Offline
Hoopy frood
Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
by setting %a to $readn, the next time the loop reads from the file, it'll start at the last line that found a match.

eg: A file with 100 lines, and matches on lines 5, 10, & 20.

Without setting %a to $readn, it'll match 5 times before getting to the next line that doesn't match

Setting %a to $readn, means that it'll find the match on 5, then look from 5 on (technically...realistically it's 6)

Regarding the other items you mentioned...agreed across the board...just wrote it up quickly.

Re: Changing a line in a txtfile #137236 10/12/05 07:55 PM
Joined: Jan 2003
Posts: 2,523
Q
qwerty Offline
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
If you're using an older version of mirc, I strongly suggest to upgrade. If you still want to stick with an old version, change $v1 to $ifmatch in the code.


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
Re: Changing a line in a txtfile #137237 10/12/05 08:01 PM
Joined: Dec 2002
Posts: 1,245
M
MikeChat Offline
Hoopy frood
Offline
Hoopy frood
M
Joined: Dec 2002
Posts: 1,245
might be a difference in style but i would do it like this
Code:
on *:text:!change & &:#:{
  var %a = 1
  var %b = $lines(file.txt)
  while (%a <= %b) {
    var %line = $replace($read(file.txt,%a),$2,$3)
    .write -l $+ %a file.txt %line
    inc %a
  }
}


seems to me that would only need 1 line 1 loop to replace all the matches (assuming data1 data2 data3 are all single words and distinctly different).
if data1 data3 both were the same (just for instance) and you wanted to change data3, these 2 scripts would change both data1 and data3 regardless, if they were the same value to start with.

Re: Changing a line in a txtfile #137238 10/12/05 08:30 PM
Joined: Sep 2003
Posts: 4,230
D
DaveC Offline
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
Quote:
just wrote it up quickly.


Way to quickly i think.
Your doing a wild search and not using "& & $2" to match the data3 items so it wont ever match (below assumes thats fixed)
Your replacement method is dodgy, it well replace data1 should it match data2, with newdata2
Your jump ahead to last line searched needed to move ahead to last line searched + 1, else it well locate a match on last line searched again
The while condition is unlikely to ever exit as should a line not be matched $readn well be 0 and thus trap %a line count of the file.

My orginal code I must admit was un checked also, i wrote it right here, however due to its small size I was pretty sure it would work, (it would of if he was using mirc 6.16 which is his fault for not mentioning he wasnt) However admittedly it doesnt cater for more than one line matching, I assumed the file had unque data3 fields, whcih might have been wrong, so i submit this to replace it.

on *:TEXT:!change & &:*:{ change $2 $3 }
alias change {
var %i = $read(txtfile.txt,nt,1), %i = 1
while (($readn) && ($read(txtfile.txt,ntw,& & $1,%i))) { write $+(-l,$readn) txtfile.txt $puttok($ifmatch,$2,2,32) | var %i = $readn | inc %i }
}

* i well admit its untested code so there might be an error in it *

PS: the inital $read of the file is to load up $readn to a none zero state