mIRC Home    About    Download    Register    News    Help

Print Thread
$read without repetition #136879 05/12/05 01:02 AM
Joined: Dec 2005
Posts: 1
P
phoenix7_1025 Offline OP
Mostly harmless
OP Offline
Mostly harmless
P
Joined: Dec 2005
Posts: 1
$read(file.txt) will pull the random line that I want, but it still leaves the possibility that the same line will be pulled on subsequent calls. I haven't been able to find a way to either delete the line when it's pulled, or somehow mark it so it won't be pulled a second time. Anyone with more experience than I with it have any ideas? Thanks.

Last edited by phoenix7_1025; 05/12/05 01:03 AM.

I dare you to make less sense.
Re: $read without repetition #136880 05/12/05 02:14 AM
Joined: Aug 2004
Posts: 7,252
R
RusselB Offline
Hoopy frood
Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
A possibility for you to consider
Code:
on *:start:{
if !$hget(text) { .hmake text 100 }
if $exists(file.txt) { .hload -n text file.txt }
}
on *:text:!read:#:{
var %line = $r(1,$hget(text,0).items)
.msg $chan $hget(text,%line)
.hdel text %line
if !$hget(text,0).items { .hload -n text file.txt }
}
  


That will load your text file into a hash table, one line per item, with each item being numbered (same as the line numbers).
When the !read command is used a random item from the hash table is generated and used to get the line from the hash table.
When the hash table is empty (ie: all of the lines have been used) then it automatically refreshes itself from the text file.

In this way, it is impossible for the same line to be read twice, but it might appear so, if two (or more) lines are identical or very similar.

Re: $read without repetition #136881 05/12/05 02:15 AM
Joined: Aug 2005
Posts: 525
S
schaefer31 Offline
Fjord artisan
Offline
Fjord artisan
S
Joined: Aug 2005
Posts: 525
You can delete the line by putting this after the call to $read

write -dl $+ $readn yourfile.txt

Re: $read without repetition #136882 05/12/05 02:47 AM
Joined: Aug 2004
Posts: 7,252
R
RusselB Offline
Hoopy frood
Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
True, they could, but that would mean having to recreate the text file every time, or having a back-up copy of the text file stored elsewhere. With my code there's no need for the extra file.

Re: $read without repetition #136883 06/12/05 05:19 AM
Joined: Sep 2003
Posts: 4,230
D
DaveC Offline
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
he said it was fine to delete the file in the original message, so it would be an acceptable and simple method.

PS you typoed!
$hget().item and not $hget().items

Re: $read without repetition #136884 06/12/05 06:19 AM
Joined: Aug 2004
Posts: 7,252
R
RusselB Offline
Hoopy frood
Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
Ooops...again...that seems to be a common typo for me...usually when I'm referencing 0 (which returns the number of entries in the hash table) <entries/items...both plural> blush