Page 1 of 1 1
Topic Options
#162688 - 2006-05-31 04:22 PM Randomize - Read and pick up quotes randomly!
1984 Offline
Starting to like KiXtart

Registered: 2003-08-14
Posts: 150
Hey,

I have a text file containing lots of “famous quotes”.
I come to the idea, make a script that randomly picks up one of the quotes from the text file and print it out to the user, each time an event is preformed

The text file:

The reason a dog has so many friends is that he wags his tail instead of his tongue.
--Unknown

History is the version of past events that people have decided to agree upon.
Napoleon Bonaparte

Here's my strategy on the Cold War: We win, they lose."
- Ronald Reagan

Maturity is achieved when a person postpones immediate pleasures for long-term values. - Joshua L. Liebman quote

And so on…

I sow the RandomizeArray() udf, but don’t know how to adapt it in this case.
Need some help on this!

Top
#162689 - 2006-05-31 05:09 PM Re: Randomize - Read and pick up quotes randomly!
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Don't randomize. That way you are going to get the same quotes repeated.

Instead do one of the following:
  • Keep a simple counter in the file and increment it each time a quote is issued. When you run out of quotes reset it to "1". This will give you a new quote each time you run it.
  • Convert the day to an internal number and use "internal_date mod number_of_quotes" to determine which quote to use. This will give you a new quote every day, but if you run the script many times on the same day you will always get the same quote. This has the added bonus that you cam make the quote file read only.

Top
#162690 - 2006-05-31 10:36 PM Re: Randomize - Read and pick up quotes randomly!
1984 Offline
Starting to like KiXtart

Registered: 2003-08-14
Posts: 150
Thanks Richard,

I would be gratfull if you could provide som example, so I could better understand what you mean in both cases and how its done.

Code:


$File = "\\Myhome\Quotes\quotes.txt"



Top
#162691 - 2006-06-01 09:26 AM Re: Randomize - Read and pick up quotes randomly!
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Well, first you need to know the format of the file.

Is it always exactly in the format of:
Code:
QUOTE
ATTRIBUTION

QUOTE
ATTRIBUTION


i.e. there is always a single line of quote followed by a single line of attribution followed by a blank line?

In quote files that I've seen there may be more than one file line in the quote.

Also, what is the size of the file? Do you care if the file is reformatted to make it easier to read? Is this for your own use (in which you can write to it) or for general use from (say) a network share in which case you will probably want it to be read only.

Top
#162692 - 2006-06-01 03:32 PM Re: Randomize - Read and pick up quotes randomly!
1984 Offline
Starting to like KiXtart

Registered: 2003-08-14
Posts: 150
I can rearrange the quotes so each follows same standard.

Conditions:

- A quote could be more then one line, but always ends with a last line containing
“-attribution”, followed by one empty line. The blank line will indicate the beginin or end of a quote.
- The quote file could be formatted as needed to make it easy to read.
- The quote file is for general use (several users) located at network share, BUT it is OK to have it open for write access if necessary. There is no need to have it read only.

i.e.

$File = “\\Myhome\Quotes\quotes.txt”

Quotes format and order in “quotes.txt”

The reason a dog has so many friends is that he wags his tail instead of his tongue.
-Unknown

History is the version of past events that people have decided to agree upon.
-Napoleon Bonaparte

Here's my strategy on the Cold War: We win, they lose."
-Ronald Reagan

Maturity is achieved when a person postpones immediate pleasures for long-term values.
-Joshua L. Liebman

Top
#162693 - 2006-06-01 05:49 PM Re: Randomize - Read and pick up quotes randomly!
1984 Offline
Starting to like KiXtart

Registered: 2003-08-14
Posts: 150
...any sample tip how to begin?
Top
#162694 - 2006-06-01 07:40 PM Re: Randomize - Read and pick up quotes randomly!
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
My recommendation since this is a per user thing would be to store a flag in the HKCU somewhere and read/write it as needed.

Then store the quote file as a .ini in the NETLOGON and read it in that format sequentially.

i.e.

[QUOTES]
001=Wicked ways are those of a wicked mind
002=Lincoln was a penny pincher

Then in the registry set 002 as the last one read and have your code increment up from there until you reach the end number then reset the value in the registry.

Top
#162695 - 2006-06-01 08:12 PM Re: Randomize - Read and pick up quotes randomly!
1984 Offline
Starting to like KiXtart

Registered: 2003-08-14
Posts: 150
Thanks Doc.

I think I got your point using an ini file and read it with ReadProfileString, instead of regular txt file.

But I’m not sure what you mean with the “flag in the HKCU… and increment up”, and how its done.

Seems I need some simple codes to study and then understand…

Top
#162696 - 2006-06-01 09:34 PM Re: Randomize - Read and pick up quotes randomly!
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
Well if no one else supplies anything I'll try to give you an example later on or tomorrow.
Top
#162697 - 2006-06-02 10:03 AM Re: Randomize - Read and pick up quotes randomly!
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Here you go:
Code:
Break ON
$=SetOption("Explicit","ON")
$=SetOption("WrapAtEOL","ON")

Dim $sQuoteTxt $sQuoteTxt=".\quote.txt"
Dim $sQuoteIni $sQuoteIni=".\quote.ini"
Dim $dTotalQuote
Dim $dTodaysQuote

If udfConvertQuoteFile($sQuoteTxt,$sQuoteIni) Exit @ERROR EndIf

$dTotalQuote=CDBL(ReadProfileString($sQuoteIni,"MAIN","TotalQuotes"))
$dTodaysQuote=($dTotalQuote+(366*Right(@YEAR,2))+@YDAYNO) mod $dTotalQuote

"Todays quote is #"+$dTodaysQuote+": "+@CRLF
udfDecode(ReadProfileString($sQuoteIni,"Q"+$dTodaysQuote,"Q"))
udfDecode(ReadProfileString($sQuoteIni,"Q"+$dTodaysQuote,"A"))

Function udfConvertQuoteFile($sFrom,$sTo) ; {{{
Dim $iCounter $iCounter=0
Dim $fh $fh=0
Dim $sLine $sLine=""
Dim $sQuote $sQuote=""
Dim $sAttrib $sAttrib=""

$udfConvertQuoteFile=0

If Exist($sTo) Exit $udfConvertQuoteFile EndIf

udfLog("Creating quote ini file...")
$fh=FreeFileHandle()
If @ERROR
$udfConvertQuoteFile=udfError("No free file handle available")
Else
If Open($fh,$sFrom)
$udfConvertQuoteFile=udfError("Cannot open original text file")
Else
$sLine=ReadLine($fh)
While @ERROR=0
If $sLine=""
If $sQuote<>""
If WriteProfileString($sTo,"MAIN","TotalQuotes",$iCounter+1)
$udfConvertQuoteFile=udfError("Cannot create new quote file")
Exit $udfConvertQuoteFile
EndIf
$=WriteProfileString($sTo,"Q"+$iCounter,"Q",$sQuote)
$=WriteProfileString($sTo,"Q"+$iCounter,"A",$sAttrib)
$iCounter=$iCounter+1
$sQuote=""
EndIf
$sAttrib=""
Else
$sQuote=$sQuote+$sAttrib
$sAttrib=udfEncode($sLIne+@CRLF)
EndIf
$sLine=ReadLine($fh)
Loop
EndIf
EndIf

If $iCounter=0 AND $udfConvertQuoteFile=0 $=Execute("Exit 2") $udfConvertQuoteFile=udfError("No messages in quote file") EndIf

Exit $udfConvertQuoteFile
EndFunction ; }}}

Function udfLog($s) ; {{{
@SCRIPTNAME+": "+$s+@CRLF
EndFunction ; }}}

Function udfError($s) ; {{{
$udfError=@ERROR
udfLog($s+" ["+@ERROR+"] "+@SERROR)
Exit $udfError
EndFunction ; }}}

Function udfDecode($s) ; {{{
While $s<>""
$udfDecode=$udfDecode+Chr(Execute("Exit &"+Left($s,2)))
$s=SubStr($s,3)
Loop
EndFunction ; }}}

Function udfEncode($s) ; {{{
While $s<>""
$udfEncode=$udfEncode+Right("00"+DecToHex(ASC($s)),2)
$s=SubStr($s,2)
Loop
EndFunction ; }}}
; vim:ai ts=4 sw=4 fdc=4 fdm=marker



This is how it works. Change $sQuoteTxt to point at your original file. Change $sQuoteIni to point to where you want the new, reformatted file to be written.

When the script runs it checks to see if $sQuoteIni exists. If it doesn't, it will create it from the $sQuoteTxt file.

The new file is in the INI style, and is lightly obfuscated so anyone looking at the file wont spoil the surprise of the quotes - you can remove the TXT file as once the INI file has been created the TXT file is no longer needed.

The selection of the quote is based on the date, everyone will get the same quote on the same date which IMO is the best way to do it. Eagle-eyed date mathematicos will spot a fault in the formula, however it is sufficiently good for your needs.

NB, once you have created the INI file there is no need to write to it again, so you can keep it on a read-only share.

I've only tested on your short sample, so give it a go with your full quote file and let us know how you get on.

Top
#162698 - 2006-06-03 03:12 AM Re: Randomize - Read and pick up quotes randomly!
1984 Offline
Starting to like KiXtart

Registered: 2003-08-14
Posts: 150
What can I say! Beautiful artwork Richard!

I’ve done quick test and it dose precisely what I had in mind.
I just love the idea with encoding/decoding.

I also tested @Time instead of @Year - (366*Left(@Time,2))+@YDAYNO) to get a quote per hour.
The txt converting to ini works without any problem.

I will now study the Convert function to understand how it works.

5 star Richard!

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 557 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.06 seconds in which 0.024 seconds were spent on a total of 13 queries. Zlib compression enabled.

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