[Scilab-users] ?==?utf-8?q? ?==?utf-8?q? ?= How to replicate what "load" does (aka creating variable in the current workspace from inside a function)

Antoine Monmayrant amonmayr at laas.fr
Mon Feb 22 12:12:05 CET 2016


 
Le Lundi 22 Février 2016 12:04 CET, "Antoine Monmayrant" <amonmayr at laas.fr> a écrit: 
 
>  
> Le Lundi 22 Février 2016 11:51 CET, Serge Steer <Serge.Steer at inria.fr> a écrit: 
>  
> > The following function does the job:
> > 
> > function myload()
> >    txt=["a=1";"b=2"];
> >    ncur= size(who("local"),"*")
> >    execstr(txt);
> >    vars=who("local");
> >    vars=vars(1:size(vars,'*')-ncur-1)
> >    args=strcat(vars,",")
> >    execstr("["+args+"]=resume("+args+")")
> > endfunction
> 
> Thank you Serge!
> It does work on 5.5.0, but it does not work on 6.6.0-beta1.
> It might be due to the way variables are ordered when calling "who".

OK, it seems that "who" no longer list variables with the most recent at the top, but in alphabetical order.
Is this a known change?
Should I fill a bug report?

Antoine

> 
> Cheers,
> 
> Antoine
> 
> 
> > 
> > Serge
> > Le 22/02/2016 11:21, Antoine Monmayrant a écrit :
> > > Hi all,
> > >
> > > I need to create variables dynamically depending on what I parse from a text file.
> > > For testing, I just made a simple script that parses the file and generates a string array that defines some variables:
> > >      txt=["a=1";"b=2"];
> > > I then
> > >      execstr(txt);;
> > > to create the variables needed (here "a" and "b").
> > > So far, so good.
> > > Now, I need to refactor my code to turn my messy script into a proper set of functions (see example code below).
> > > The issue now is that I can't figure out how to define "a" and "b" from within the function.
> > > I tried to declare them as "global" inside the function, but it does not work: as I haven't  declared them global at the top level before calling my function, it cannot work.
> > > Sadly, as I don't know in advance the variables I'll find when I parse the file, I cannot declare the variables global at the top level.
> > >
> > > Any solution or workaround?
> > >
> > > Cheers,
> > >
> > > Antoine
> > >
> > > /////example code
> > >
> > > //script version:
> > >
> > > clear a b;
> > > exists("a")// nope
> > > exists("b")// nope
> > > txt=["a=1";"b=2"];//typical result from parsing my txt file
> > > execstr(txt);
> > > exists("a")// yes
> > > exists("b")// yes
> > >
> > > //now with a function
> > >
> > > //broken & useless
> > > function myload()
> > >      txt=["a=1";"b=2"];
> > >      //string to declare a and b global
> > >      globaltxt="global "+strsubst(txt, '/=.*/', ';','r');
> > >      //does not work
> > >      execstr(globaltxt);
> > >      execstr(txt);
> > > endfunction
> > >
> > >
> > > //does not work
> > > clear a b;
> > > exists("a")// nope
> > > exists("b")// nope
> > > myload()//
> > > exists("a")// nope
> > > exists("b")// nope
> > >
> > >
> > > //this will work
> > > clear a b
> > > exists("a")// nope
> > > exists("b")// nope
> > > global a b // well I can't do that in practice, I don't know a and b in advance
> > > myload()//
> > > exists("a")// nope
> > > exists("b")// nope
> > >
> > >
> > >
> > > _______________________________________________
> > > users mailing list
> > > users at lists.scilab.org
> > > http://lists.scilab.org/mailman/listinfo/users
> > >
> > 
> > _______________________________________________
> > users mailing list
> > users at lists.scilab.org
> > http://lists.scilab.org/mailman/listinfo/users
> > 
>  
>  
>  
>  
> 
> 
> _______________________________________________
> users mailing list
> users at lists.scilab.org
> http://lists.scilab.org/mailman/listinfo/users
> 
 
 
 
 





More information about the users mailing list