#162689 - 2006-05-31 05:09 PM
Re: Randomize - Read and pick up quotes randomly!
|
Richard H.
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
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
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
|
|
|
|
#162697 - 2006-06-02 10:03 AM
Re: Randomize - Read and pick up quotes randomly!
|
Richard H.
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
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
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 557 anonymous users online.
|
|
|