#141789 - 2005-06-16 09:01 AM
How to get line from text file randomly?
|
PMaric
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
   
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
|
|
|
|
#141793 - 2005-06-16 02:13 PM
Re: How to get line from text file randomly?
|
Jochen
KiX Supporter
   
Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
|
|
|
Top
|
|
|
|
#141794 - 2005-06-16 02:47 PM
Re: How to get line from text file randomly?
|
Richard H.
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
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
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
|
|
|
|
#141799 - 2005-06-20 10:32 AM
Re: How to get line from text file randomly?
|
Richard H.
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
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 2220 anonymous users online.
|
|
|