Page 1 of 2 12>
Topic Options
#39871 - 2003-05-02 07:06 PM ReadLine Function
Stryder Offline
Lurker

Registered: 2003-04-28
Posts: 2
Loc: Kansas
I need to be able to have a script read a text file and capture the value of each line in the file. I know how to read a line with readline or you can even manually add another readline ($x = readline + readline) and get the first two lines of the text file. Is there a way to have the script determine how many readline's it needs to perform to get the entire contents of the file?

Below is an example of what I am working with and I have the script manually reading the first two lines of the file.

If Exist ("\\pathtofile\$month$day.txt")
$msgtext = $msgtext + 'Birthdays for today are:' + chr(10)
IF Open(3, "\\pathtofile\$month$day.txt") = 0
$x = ReadLine(3) + chr(10) + ReadLIne(3)
Close (3)
EndIf
$msgtext = $msgtext + chr(10) + $x + chr(10) + chr(10)
EndIf

I then use a messagebox to display the value of $msgtext.

Can someone help?

Thanks

Top
#39872 - 2003-05-02 07:10 PM Re: ReadLine Function
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
could you not do:
$x=readline(1)
while not @error
? $x
$x=readline(1)
loop
? "end of file"
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#39873 - 2003-05-02 07:21 PM Re: ReadLine Function
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
It would be much more convenient to put this type of info into an .INI file
code:
;birthdays.ini
[Birthdays]
01/01/1990=John Doe, Jane Doe
;or
[01/01/1990]
John Doe=Marketing
Jane Doe=Sales

_________________________
There are two types of vessels, submarines and targets.

Top
#39874 - 2003-05-02 11:05 PM Re: ReadLine Function
New Mexico Mark Offline
Hey THIS is FUN
****

Registered: 2002-01-03
Posts: 223
Loc: Columbia, SC
It may not be clear from Sealeopard's post (some of the folks on this board cruise along somewhere in the scripting stratosphere), but if you are not conversant with the built-in readprofile() and writeprofile() functions, you should familiarize yourself with them. They may be perfect for what you are trying to do and could save you a lot of effort.

NMM

Top
#39875 - 2003-05-03 01:15 AM Re: ReadLine Function
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
An even better .INI file structure would be
code:
[January]
1=John Doe
5=Jane Doe
[February]
6=John Miller, Jack Black

You wold then require only one line of code to get the birthdays for a particular day, without even indicating the true age of the persons.
_________________________
There are two types of vessels, submarines and targets.

Top
#39876 - 2003-05-03 08:18 AM Re: ReadLine Function
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11629
Loc: CA
Hello Brian,

Here is an example of doing what you're looking to do.
You need to supply the real path to the BIRTHDAYS.INI file
and you need to fill it out to include ALL MONTHS and ALL DAYS
as well as ALL USERS with their Birthday on that month and day.

It is possible to do this other ways, but this should suffice or at least give you a little more insight on how it can be done.

Hope this helps, if you have questions please don't hesitate to ask. The Piranha members around here will be happy to help. [Smile]

DisplayBirthdays.kix



$Y = SUBSTR(@DATELEN(@DATE) - 94)
$M = SUBSTR(@DATELEN(@DATE) - 42)
$D = SUBSTR(@DATELEN(@DATE) - 12)
$MyFile=("C:\TEMP\birthdays.ini")
$Birthdays=ReadProfileString ($MyFile$M$D)
$AlertBD=MessageBox("Today is "+@DATE+@CRLF+"Happy Birthday to "+$Birthdays"Happy Birthday"64)



birthdays.ini


[01]
01=John Doe
02=Bill Smith
03=Brian Wilson
04=George Garthy
05=Jane Doe
[02]
01=John MillerJack Black
02=Karen Gleason
[03]
01=Wendy Feiten
02=Brad Jones
[04]
01=Greg Engle
02=Tim Vinter
03=Beth Angel
[05]
01=Carl Celt
02=Kevin EmpireAndy Burke
03=Helen Birch
04=Celine WelshBart Sordel


Top
#39877 - 2003-05-04 12:25 AM Re: ReadLine Function
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Here is one of the Piranhas playing KiXGolf with it :



break on

$f   = @scriptdir + '\Birthdays.ini'
$BDs = readprofilestring($fright('0'+@monthno,2), right('0'+@mdayno,2) )
if $BDs
    $_ = messagebox("Todays Birthdays are:" + @crlf + $BDs"Happy Birthday"64)
endif



{partially 'borrowed' from Richard}
_________________________



Top
#39878 - 2003-05-03 01:16 PM Re: ReadLine Function
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
you shall not say KiXgolf in vain.

code:
$BDs = readprofilestring(@scriptdir + '\Birthdays.ini', right('0@monthno',2), right('0@mdayno',2) )
if $BDs
$_ = messagebox("Todays Birthdays are:@crlf" + $BDs, "Happy Birthday", 64)
endif

no real parsing has done.
just removed some unneeded stuff.

[ 03. May 2003, 13:17: Message edited by: Lonkero ]
_________________________
!

download KiXnet

Top
#39879 - 2003-05-03 07:55 PM Re: ReadLine Function
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
code:
$bd=readprofilestring(@SCRIPTDIR+'\birthdays.ini',@MONTH,@MDAYNO)
if $bd
$rc=messagebox('Todays Birthdays are:@crlf' + $bd, 'Happy Birthday', 64)
endif

for the following BIRTHDAY.INI
code:
[January]
1=John Doe
5=Jane Doe
[February]
6=John Miller, Jack Black

Granted, the .INI file is longer, but more readable and the KiXtart code itself is shorter [Big Grin]
_________________________
There are two types of vessels, submarines and targets.

Top
#39880 - 2003-05-03 09:42 PM Re: ReadLine Function
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11629
Loc: CA
See Brian,

What did I tell you about those Piranha.

All these good ideas. Let me recommend though, that regardless of who's idea/code you use. Make sure you liberally comment the code so that in the future you'll know what and how it does it.

Code that is left alone for long periods of time can be difficult to decipher unless you work with it daily. I know some Admins write stuff and don't look at it or touch it for Months at a time, then when they go back they have trouble knowing what it does because they don't script or code daily. Unlike some of the Piranha here who play with it daily and can get away with this KiX Golf coding style (though if done in production, the next Admin to come along won't be happy with them [Eek!] ) Commenting code is a Best Practice but few do it.

Top
#39881 - 2003-05-04 12:06 AM Re: ReadLine Function
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
sure!
who wants to give commented code on silver plate?
sure everyone wants leave some pain behind them for the case they get fired!
_________________________
!

download KiXnet

Top
#39882 - 2003-05-04 04:23 AM Re: ReadLine Function
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11629
Loc: CA
I hope not. I for one do not share that attitude. You've handed nothing on a Silver Platter at work, but rather you've done your job, which in my opinion should include allowing others to easily assist you or take over the job in case you are injured, promoted, or laid off.

Why would you want someone else to suffer? Just don't see the reason why one human being would want another to suffer when they had done nothing to you. [Confused]

Top
#39883 - 2003-05-04 06:17 AM Re: ReadLine Function
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
nothing directly...

mm... someone else to assist me...
hasn't happened but that's good point.

anyway, my code is cryptic and I don't make it easy to adapt for anyone else.
thus, me suffering does not help anyone else.
but, does not cause any pain either...

my code does not stop to work if I'm not present [Big Grin]
_________________________
!

download KiXnet

Top
#39884 - 2003-05-04 01:33 PM Re: ReadLine Function
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Actually, I do need "code on a silver platter" for myself, as I do tend to forget why I wrote a certain routine in a specific way. Especially if you suddenly have to reexamine code you've written two or three years ago.
_________________________
There are two types of vessels, submarines and targets.

Top
#39885 - 2003-05-04 01:58 PM Re: ReadLine Function
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Boy, this touched a nerve!

I've got to agree with Doc on this one. I've posted code heer in the past and had several (good) suggestions about making it shorter. I've also seen several examples posted here that I had to scratch my head on... Remember which forum we're in!!

Here, simple code is usually better. It's OK to follow it with a comment that "blah-blah" is a shorter way to write it, but will new users really get it (today)?

I'm in the midst of extending the comments in my code and then writing documentation on the coding as well as the operation, because staff members at two other main sites have been assigned to be my "scripting backup" - in case I get hit by a bus.

Anyway, just remember which forum you guys are in and describe how your cool code works when you post it.. maybe even say that your code replaces such-and-such lines of code so the new guy can actually learn about KixGolf concepts. [Wink]

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#39886 - 2003-05-04 08:27 PM Re: ReadLine Function
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Can see both sides of the coin here.. Documentation is good in the event you are no longer able to perform script functions.. However, there is a good point, that if you "squeeze" the script down, the time it takes to parse it through will be less. This, then begs the question at what expense? Something so cryptic that nobody understands? Or, is just something infinitessimal?

So, for example, you take the script from 40ms to 20ms, is that a good trade-off? These numbers are still less than a second.

Thanks,

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#39887 - 2003-05-04 10:03 PM Re: ReadLine Function
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
these last replies have been pretty informative...

seems that the amount of coders who really know what they are doing when they put letters together is pretty small in the kixtart-expert field.

glenn on the other hand is little bit different as if he really has backup-coders.
for those indeed it's good to have some docs.

anyway, if you have say UDF that does work, why every newbie should need to understand it after 30 years?
the main point is to produce working code which is reliable and fast.

not saying that on KORG you can't post well documented code, but saying that for your own usage it's best practise to comment, I have to disagree.
it's totally different if ppl forget what their yesterday's code does and are unable to read kixtart code which is mix of english and math.
then, sure, for your own best, you should do that... or start eating b-vitamin to get your memory better.
_________________________
!

download KiXnet

Top
#39888 - 2003-05-05 11:02 PM Re: ReadLine Function
New Mexico Mark Offline
Hey THIS is FUN
****

Registered: 2002-01-03
Posts: 223
Loc: Columbia, SC
The previous comments raise a question about how KiXtart tokenizes and runs scripts. IIRC, the script is initially parsed and tokenized to memory, then run from there. Granted, a ton of comments might slow down the this phase slightly, but should make no difference in the script execution speed once complete.

Personally I'd rather wait a few milliseconds more to load well-documented code than to spend much more time unraveling poorly-documented code (even my own) before making a change.

NMM

Top
#39889 - 2003-05-05 11:38 PM Re: ReadLine Function
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
...and use a lot more time doing that good documentation/commenting.
_________________________
!

download KiXnet

Top
#39890 - 2003-05-05 11:43 PM Re: ReadLine Function
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Wouldn't this be a good opportunity to use something like KIXREF and spool this info to a separate file? The only downfall to that would be is that if you had any updates, you would have to re-run it.

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
Page 1 of 2 12>


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

Who's Online
0 registered and 988 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.074 seconds in which 0.027 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