Variable convention questions

Klaus Major klaus.major at metascape.org
Fri Sep 27 08:32:01 CDT 2002


Hi Fred,

> I have just hand-rolled my first real handler/function.

Congrats and welcome to the club :-)

> It, puts the list of
> files that end in "txt" which are in the same folder/directory as the 
> stack into
> a variable called theList.
> -----------------
> on buildPickList theList
>   set the itemDelimiter to "/"
>   set the defaultFolder to item 1 to -2 of the effective filename of 
> this stack
>   put the files into theFileList
>   repeat for each line thisLine in theFileList
>   if char -3 to -1 of thisLine is "txt" then put thisLine & return 
> after theList
>   end repeat
> end buildPickList
> -----------------
>
> Since I am new to programming and Rev I've got questions regarding 
> variables. I
> haven't declared the variables at the beginning of the function. Is 
> this good
> practice?

It is not necessary to declare a variable that will only be used in one 
handler.
The "mouseup" handler in this case.

If a variable needs to be valid in a complete script then you have to 
declare it
as a "local":

##########
local yourvar

on mouseneter
   put fld 1 into yourvar
end mouseener

on mouseup
   ### do somtehing with yourvar
end mouseup

on mouseleave
   put yourvar into fld 1
end mouseleave

etc...
############

If you need the value of a variable in all your scripts in all your 
stacks
(during one RR-session) declare it as global.

See the index for "local" and "global".

> There is a variable created in this line "repeat for each line 
> thisLine in
> theFileList" which  I filched the method from the "repeat" section of 
> the
> Transcript dictionary. Why should it work?  I would have thought I 
> would have
> had to do something like below (except "this" is reserved for cards 
> and stacks)
> :
>
> "repeat for each line of theFileList
>   set theLine to this line... "
>
> It seems logically inconsistent to create variable simply by placing 
> it after
> "repeat for each line". Why does it work?

Sounds like a philosophical question ;-)
Because the engine is programmed that way.

> Aside the above can the function be improved/made more efficent?

Yes :-)

on buildPickList theList
   set the itemDelimiter to "/"
   set the defaultFolder to item 1 to -2 of the effective filename of 
this stack
   put the files into theFileList
   filter theFileList with "*.txt"
   ## this will only leave files ending with "*.txt" in theFileList
   ## and you can save the variable "theList" for bad times or winter ;-)
   ## read "is not necessary anymore"
end buildPickList

> TIA, Fred D

You're welcome.

Have a nice weekend


Regards

Klaus Major
klaus.major at metascape.org




More information about the use-livecode mailing list