Re: problems with persistence




"REIDAR" <aureid@xxxxxxxxx> schrieb im Newsbeitrag
news:ec8e5922-1146-446c-b8ed-b7a72dfb19ef@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
To put it simply, my persistent variables aren't persisting (between
function calls)

I've attached my code, but I'll describe what is going wrong. When I
start up my program, I tell the function to 'clear'. This clears all
the values, and reinitializes everybody. Later on, when I try to
store a value, it doesn't have the variables in memory, so it
reinitializes them, wiping out the structure, and leading to a whole
pantload of error messages.

I understood that the whole point of persistant variables is that they
persist. What am I doing wrong?

My code has a little method for storing everything, it looks like
this:

function out=database(direction,name,value)
switch direction
case 'clear'
out=-1;
clear stacks names index
persistent stacks names index
stacks=struct('matrix',[]);
names=struct('string',[]);
index=0;
case 'return'
switch name
case 'stacks'
out=stacks;
return
case 'names'
out=names;
return
case 'index'
out=index;
return
end
case 'store'
out=0;
index=index+=1;
switch name
case 'stacks'
stacks(index).matrix=value;
return
case 'names'
names(index).matrix=value;
return
case 'index'
index=value;
return
end
end


Hi,
move the persistent to the front:

function out=database(direction,name,value)
persistent stacks names index
switch direction
case 'clear'
out=-1;
stacks=struct('matrix',[]);
names=struct('string',[]);
index=0;
case 'return'
....

BTW: for such things I would take a look at nested functions. Especially if
you need to have more than one database...

Titus


.



Relevant Pages

  • Re: problems with persistence
    ... REIDAR writes: ... the values, and reinitializes everybody. ... persistent stacks names index ...
    (comp.soft-sys.matlab)
  • Re: RfD - N>R and NR>
    ... Stacks are more attractive and natural in Forth than ALLOCATE/FREE. ... But it's not a general solution. ... If there's limited RAM then you need to store intermediate results ... If you run out of disk space there ...
    (comp.lang.forth)