Page 1 of 1 1
Topic Options
#141789 - 2005-06-16 09:01 AM How to get line from text file randomly?
PMaric Offline
Lurker

Registered: 2005-06-16
Posts: 3
Hi Guys,

How would I be able to make a KiXtart script that will pull a couple of lines from a text file randomly?

So it will go to textfile.txt and get 2 random consecutive lines and display them.

Any help would be appreciated.

Regards
Paul

Top
#141790 - 2005-06-16 10:11 AM Re: How to get line from text file randomly?
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
The detail will depend on whether the lines are pairs or if you just want two consecutive lines, but simply:
  • Read the file into an array (see the ReadFile() UDF in the UDF section)
  • Count the number or lines (UBound())
  • Use RND() & SRND() to get a line number within the range.
  • Display random line and random+1 line from the array.

Top
#141791 - 2005-06-16 10:23 AM Re: How to get line from text file randomly?
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Something like this maybe ?

Code:

break on

srnd(@ticks)

$file = @scriptdir + '\test.txt'
$_ = open(1,$file)

;read contents of file to a delimited string value
$txt = readline(1)
while not @error
$txt = $txt + "~" + readline(1)
loop
$_ = close(1)

$txt = left($txt,-1) ;remove last delimiter
$txt = split($txt,'~') ;split that string to an array

$rand = rnd(ubound($txt))
"Random line #1: " + $txt[$rand] ?
"Random line #2: " + $txt[$rand+1] ?

get $
exit 0



Note: didn't bother to implement good coding practices in that sample like NoVarsInStrings and Explicit set ot on

[edit: consecutive means then that the second line needs to be the line after the first then? K, edited code to match that]


Edited by Jochen (2005-06-16 10:29 AM)
_________________________



Top
#141792 - 2005-06-16 01:54 PM Re: How to get line from text file randomly?
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Careful!

If the first random line is the last line in the file you will get an out-of-bounds for random+1.

Top
#141793 - 2005-06-16 02:13 PM Re: How to get line from text file randomly?
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Dang

Sure thing

k,

Code:

break on

srnd(@ticks)

$file = @scriptdir + '\test.txt'
$_ = open(1,$file)

;read contents of file to a delimited string value
$txt = readline(1)
while not @error
$txt = $txt + "~" + readline(1)
loop
$_ = close(1)

$txt = left($txt,-1) ;remove last delimiter
$txt = split($txt,'~') ;split that string to an array

$rand = rnd(ubound($txt))
if $rand = ubound($txt) $rand = $rand - 1 endif
"Random line #" + $rand+1 + ": " + $txt[$rand] ?
"Random line #" + $rand+2 + ": " + $txt[$rand+1] ?

get $
exit 0



Well, make sure that there always at least 2 lines of text in your file


Edited by Jochen (2005-06-16 02:15 PM)
_________________________



Top
#141794 - 2005-06-16 02:47 PM Re: How to get line from text file randomly?
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Easier to do:
Code:
$rand = rnd(ubound($txt)-1)



BTW, if the lines are significant pairs (i.e. you want lines 1+2, 3+4, 5+6 and so on but never lines 2+3) the "random" line should look like this:
Code:
$rand = 2 * rnd(Int(ubound($txt)/2))


Top
#141795 - 2005-06-20 12:35 AM Re: How to get line from text file randomly?
PMaric Offline
Lurker

Registered: 2005-06-16
Posts: 3
Thank you for the replies guys.

Just one last thing I would like to do with the script, can I get it to select the two lines of consecutive text each day?

So each day it selects a new line, so when one user logs on Tuesday, they see the selection for Tuesday, along with any other user that logs on that day?

Regards
Paul

Top
#141796 - 2005-06-20 01:02 AM Re: How to get line from text file randomly?
PMaric Offline
Lurker

Registered: 2005-06-16
Posts: 3
Also, with the current script, I tried throwing that into the kiXtart logon script and I get an error saying:

"Script error: array reference out of bounds!
"Random Line #" + $rand+1 + " + $txt[$rand] ?"

Top
#141797 - 2005-06-20 01:24 AM Re: How to get line from text file randomly?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
What ver @KiX?
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#141798 - 2005-06-20 01:40 AM Re: How to get line from text file randomly?
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
And what's your current script? Did you make modifications to the example? Does the text file have at least two lines?
_________________________
There are two types of vessels, submarines and targets.

Top
#141799 - 2005-06-20 10:32 AM Re: How to get line from text file randomly?
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Code:
So each day it selects a new line, so when one user logs on Tuesday, they see the selection for Tuesday, along with any other user that logs on that day?



This doesn't sound like a requirement for random results to me, it sounds more like you just want a new message each day.

If this is the case, use an INI file to record the message for the day.

You need two entries in the file - one is the cureent message number and the other is the date that the message was used. If the date <> today, add two to the message counter (wrap to zero when you reach the end of the file) and set the used day to today.

Here is an example (untested):
Code:
Break ON
$=SetOption("Explicit","ON")
Dim $sIni,$sMOTD,$fhMOTD,$asData,$sRecord,$sUsedDay,$iMessage

; If the INI file does not exist it will be created
$sINI=@SCRIPTDIR+'\MessageOfTheDay.ini'
; MOTD file contains all the empowering messages
$sMOTD=@SCRIPTDIR+'\MessageOfTheDay.txt'

$fhMOTD=FreeFileHandle()
If @ERROR
"Cannot allocate a free file handle [" +@ERROR+"] "+@SERROR ?
Else
If Open($fhMOTD,$sMOTD)
"Cannot open message file for reading ["+@ERROR+"] "+@SERROR ?
Else
$sRecord=readline($fhMOTD)
While Not @ERROR
$asData=$asData+$sRecord+Chr(10)
$sRecord=readline($fhMOTD)
Loop
$=Close($fhMOTD)
$asData=Split(Left($asData,-1),Chr(10))
$sUsedDay=ReadProfileString($sINI,"CONTROL","LastUsed")
$iMessage=Val(ReadProfileString($sINI,"CONTROL","Message"))
If $sUsedDay="" $sUsedDay=@DATE EndIf
If $sUsedDay<>@DATE
$iMessage=$iMessage+2
If $iMessage>=UBound($asData) $iMessage=0 EndIf
$=WriteProfileString($sINI,"CONTROL","LastUsed",@DATE)
$=WriteProfileString($sINI,"CONTROL","Message",$iMessage)
EndIf
">> "+$asData[$iMessage] ?
">> "+$asData[$iMessage+1] ?
EndIf
EndIf



You will need to ensure that your users can write to the INI file during logon.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 2220 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.076 seconds in which 0.037 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org