mIRC Home    About    Download    Register    News    Help

Topic Options
#137230 - 10/12/05 02:08 PM Changing a line in a txtfile
bapplander Offline
Ameglian cow

Registered: 29/03/05
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 ?

Top
#137231 - 10/12/05 02:52 PM Re: Changing a line in a txtfile
DaveC Offline
Planetary brain

Registered: 26/09/03
Posts: 4230
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) } }

Top
#137232 - 10/12/05 04:14 PM Re: Changing a line in a txtfile
bapplander Offline
Ameglian cow

Registered: 29/03/05
Posts: 39
hm..it seems to only delete the line, not replacing it with something new.. shocked

Top
#137233 - 10/12/05 05:26 PM Re: Changing a line in a txtfile
RusselB Offline
Planetary brain

Registered: 03/08/04
Posts: 7252
Loc: Ontario, Canada
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
  }
}
 

Top
#137234 - 10/12/05 06:01 PM Re: Changing a line in a txtfile
MikeChat Offline
Hoopy frood

Registered: 12/12/02
Posts: 1245
Loc: California, doooood
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
  }
}

Top
#137235 - 10/12/05 06:21 PM Re: Changing a line in a txtfile
RusselB Offline
Planetary brain

Registered: 03/08/04
Posts: 7252
Loc: Ontario, Canada
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.

Top
#137236 - 10/12/05 07:55 PM Re: Changing a line in a txtfile
qwerty Offline
Hoopy frood

Registered: 07/01/03
Posts: 2523
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

Top
#137237 - 10/12/05 08:01 PM Re: Changing a line in a txtfile
MikeChat Offline
Hoopy frood

Registered: 12/12/02
Posts: 1245
Loc: California, doooood
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.

Top
#137238 - 10/12/05 08:30 PM Re: Changing a line in a txtfile
DaveC Offline
Planetary brain

Registered: 26/09/03
Posts: 4230
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

Top