Re: [S] Global variables in Splus?

Alan Zaslavsky (zaslavsk@hcp.med.harvard.edu)
Wed, 25 Mar 1998 07:21:17 -0500 (EST)


> From: Steve Roberts <sroberts@PICR.man.ac.uk>
> ... So I want to increment some sort of "global"
> variable, a concept which S-plus does not have - something like the good
> old-fashioned Fortran common block!!! I thought I could do something
> with frames but got nowhere.

You were on the right track. The usual technique in this situation
is to assign in frame 0 or frame 1.

e.g.

assign("total.time",0,fr=0)

assign("total.time",get("total.time",fr=0)+added.time,fr=0)

The difference between assigning in frame 0 and frame 1 is that the former
persists for the duration of the session but the latter disappears at the
end of the evaluation of a top-level expression. See the following example.

> for(i in 1:4) {
+ if(i==1) assign("goop",100,fr=0) else
+ assign("goop",get("goop",fr=0)+i,fr=0);+ print(get("goop",fr=0));+ }
[1] 100
[1] 102
[1] 105
[1] 109
> print(get("goop",fr=0))
[1] 109
> > for(i in 1:4) {
+ if(i==1) assign("guup",100,fr=1) else
+ assign("guup",get("guup",fr=1)+i,fr=1);+ print(get("guup",fr=1));+ }
[1] 100
[1] 102
[1] 105
[1] 109
> print(get("guup",fr=1))
Error in get.default("guup", fr = 1): Object "guup" not found
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news