This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

random line

Old posts that have not been replied to for several years.
Locked
z
zleppy
Voice
Posts: 13
Joined: Wed Apr 14, 2004 2:32 pm

random line

Post by zleppy »

Hey, i'm trying to make my own triviascript (only found buggy ones that would crash my bot), and i want the script to count the lines in a file, and then choose a random line...anyone know how i could do this?

-zleppy-
User avatar
KrzychuG
Master
Posts: 306
Joined: Sat Aug 16, 2003 2:51 pm
Location: Torun, Poland
Contact:

Post by KrzychuG »

Should work:

Code: Select all

proc show:random { file } {
  set fname [open $file r]
  set num 0
  while {![eof $fname]} {
    incr num
    set line($num) [gets $fname]
  }
  putloglev o * "All lines in $file: $num"
  if {$num > 0} then {
    putloglev o * "Random line: $line([rand $num])
  }
} ;# show:random
e
egghead
Master
Posts: 481
Joined: Mon Oct 29, 2001 8:00 pm
Contact:

Post by egghead »

Another way is to determine the file size, set a pointer to a random value between 0 and that file size, open the file, read the line at the random pointer and close the file.
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

to get number of lines try this

Code: Select all

proc length {file} {
  set rf [open $file r]
  set length [llength [split [read $rf] \n]]
  close $rf
  return $length
}
like this you can use it in rand mutch more easyer ..

then do

Code: Select all

proc getline {file} {
  set rf [open $file r]
  foreach x [split [read $rf] \n] {
    if {[info exists i]} {
      incr i
    } else {
      set i 0
    }
    if {[string equal $i [rand [length $file]]]} {
      set getline [join [lrange $x 0 end]]
      if {[string equal {} [lindex $x 0]]} {
        getline $file
        break
      } else {
        break
        return $getline
      }
    }
  }
  close $rf
}
not tested it tho could be an problem with the if {} string .. second one.. when u restart the proc ..
XplaiN but think of me as stupid
g
greenbear
Owner
Posts: 733
Joined: Mon Sep 24, 2001 8:00 pm
Location: Norway

Post by greenbear »

this is also covered in the faq section on this forum

http://forum.egghelp.org/viewtopic.php?t=6885
User avatar
caesar
Mint Rubber
Posts: 3776
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

and had been discussed before. :mrgreen:
Once the game is over, the king and the pawn go back in the same box.
z
zleppy
Voice
Posts: 13
Joined: Wed Apr 14, 2004 2:32 pm

Post by zleppy »

sorry for taking it up again then..just so much work searching the old posts...
i got it fixed now...now only thing is to set the a globa variable with "the answer", so i can check it laters...
would 'set answer "mytxt"' work?
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

sorry for taking it up again then..just so much work searching the old posts...
hmm don't wana be a pain but if u don't bother reading old posts why should we bohter answering ??
would 'set answer "mytxt"' work?
if up put it in the proc yes if not u need to use global to import it !!
XplaiN but think of me as stupid
z
zleppy
Voice
Posts: 13
Joined: Wed Apr 14, 2004 2:32 pm

Post by zleppy »

ok..thanks...sorry, dont mean to be rude when saying that i dont "bother", but earlier when i have searched, i have found like 1500 posts, and not found anything helpfull....
but thanks for answering =)
Locked