[NTG-context] Metafun : save memory content ?

Fabrice L fabrice.alpha at gmail.com
Wed Jul 20 05:01:22 CEST 2022


Dear Bruce, dear list, 

> Le 19 juill. 2022 à 07:42, Bruce Horrocks <ntg at scorecrow.com> a écrit :
> 
>> On 19 Jul 2022, at 04:28, Fabrice L via ntg-context <ntg-context at ntg.nl> wrote:
>> 
>> Complementary question : I saw that the « pos_a » « pos_b » of the example could not take numerical value, for example pos_1, pos_2.. How can I use metafun to write a macro to save the contents of hundreds of variable in this object, something like :
>> 
>> getparameters "MyData » [
>> 	for i=1 upto 100 :
>> 		pos_decimal(i) = ThePosition[i],
>> 	endfor ;
>> ] ;
>> 
>> If this is possible, then this is wonderful and open news doors ! 
> 
> I've adapted this example from the metafun-p manual by adding some "write" statements.

I have read the manuals, but not enough it seems ! I missed this part ! I completely miss the «write to » and « read from » instructions. Even a research on the web before posting did not return something interesting. I guess I did not used the good terms, because now, I find some ressources about these functions ! 
> 
> \starttext
> \startMPpage
> 
> numeric done[][], i, j, n ; n := 0 ;
> forever :
> 	i := round(uniformdeviate(10)) ;
> 	j := round(uniformdeviate(10)) ;
> 	if unknown done[i][j] :
> 		drawdot
> 			(i*cm,j*cm)
> 			withpen pencircle scaled 0.5cm
> 			withcolor darkred;
> 		n := n + 1 ;
> 		done[i][j] := n ;
> 		
> 		write "done[" & tostring(i) & "][" & tostring(j) & "] := " & tostring(n) & " ;" to "mp_test_file.txt" ;
> 
> 	fi ;
> 	exitif n = 10 ;
> 	
> endfor ;
> 
> write EOF to "mp_test_file.txt" ;
> 
> \stopMPpage
> \stoptext
> 
> This results in the expected graphic but also a local file (in the same directory as the source) called "mp_test_file.txt" that contains:
> 
> done[9][1] := 1 ;
> done[4][3] := 2 ;
> done[5][3] := 3 ;
> done[5][1] := 4 ;
> done[2][9] := 5 ;
> done[8][6] := 6 ;
> done[9][10] := 7 ;
> done[9][7] := 8 ;
> done[10][2] := 9 ;
> done[6][5] := 10 ;
> 
> I think, with a bit of judicious use of "write" statements to add some ConTeXt / MP setup code, it would be possible to include the output from the previous run as MP source of the next run.
> 
> Hope this helps, or at least provides some food for thought.

Totally, a lot of food  ! Here is my yesterday example with these functions : 

\starttext
\startMPpage
pair ThePosition[];
ThePosition[1] := (0,0) ;
ThePosition[2] := (10,10) ;
draw ThePosition[1] -- ThePosition[2] ;

% Save the position into a file "bruce.mp"
for i=1 upto 2:
  write "ThePosition[" & tostring(i) & "] := (" & tostring(xpart(ThePosition[i])) & ","& tostring(ypart(ThePosition[i])) &") ;" to "bruce.mp" ;
endfor;
write EOF to "bruce.mp" ;
\stopMPpage

% The external file would like like (comment added manually here):
% ThePosition[1] := (0,0) ;
% ThePosition[2] := (10,10) ;

% We modify the value of ThePosition[2] 
\startMPpage
ThePosition[2] := (10,0) ;
draw ThePosition[1] -- ThePosition[2] ;
\stopMPpage

% We now read the value of ThePosition[2] from the file 
\startMPpage
input "bruce.mp";
% Save the data from the external file 
draw ThePosition[1] -- ThePosition[2] ;

\stopMPpage

\stoptext

> 
> (Note you need to delete the mp_test_file.txt before re-running else it fails.)

So this totally do the trick : once again, magic is happening ! 
Note that in my installation, I do not need to delete the external file. 
> 
>> Bruce Horrocks
> Hampshire, UK

Thanks a lot ! 
Fabrice. 
> 



More information about the ntg-context mailing list