From: Fabrice L via ntg-context <ntg-context@ntg.nl>
To: Bruce Horrocks <ntg@scorecrow.com>
Cc: Fabrice L <fabrice.alpha@gmail.com>,
mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: Re: Metafun : save memory content ?
Date: Tue, 19 Jul 2022 23:01:22 -0400 [thread overview]
Message-ID: <0F27AEF6-4AF8-4E3B-8B3A-75F4D82E703D@gmail.com> (raw)
In-Reply-To: <860FECFA-64D4-4354-AE07-91BCADA6EBC7@scorecrow.com>
Dear Bruce, dear list,
> Le 19 juill. 2022 à 07:42, Bruce Horrocks <ntg@scorecrow.com> a écrit :
>
>> On 19 Jul 2022, at 04:28, Fabrice L via ntg-context <ntg-context@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.
>
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage : https://www.pragma-ade.nl / http://context.aanhet.net
archive : https://bitbucket.org/phg/context-mirror/commits/
wiki : https://contextgarden.net
___________________________________________________________________________________
prev parent reply other threads:[~2022-07-20 3:01 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-18 13:20 Fabrice L via ntg-context
2022-07-18 14:39 ` Henning Hraban Ramm via ntg-context
2022-07-18 15:45 ` Hans Hagen via ntg-context
2022-07-18 16:31 ` Hans Hagen via ntg-context
2022-07-18 17:53 ` Hans van der Meer via ntg-context
2022-07-19 3:28 ` Fabrice L via ntg-context
2022-07-19 11:42 ` Bruce Horrocks via ntg-context
2022-07-20 3:01 ` Fabrice L via ntg-context [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=0F27AEF6-4AF8-4E3B-8B3A-75F4D82E703D@gmail.com \
--to=ntg-context@ntg.nl \
--cc=fabrice.alpha@gmail.com \
--cc=ntg@scorecrow.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).