Script

From J Wiki
Jump to navigation Jump to search


<=   =>

When you close J you lose the definitions of all the names. What you execute in the Term window affects the current session, but is not permanent. This is fine when experimenting, but when you start defining things like your centigrade verb you want to record the definition so that you can use it in another session.

Close J and restart it.

   centigrade
|value error: centigrade

You have a clean slate. The definition of centigrade, and all the other names you defined, in the previous session are lost.

At least the primitives are still there!

   2 + 5
7

As you would expect, to maintain a permanent record of your definitions, you save them in files. Files with J sentences and definitions are called script files and you can edit them just as you would edit any other text file. Script files typically have a suffix of .ijs.

Remember: a script file is a source file for definitions.

Although you can use any text editor to work with script files, the J system provides a simple editor that is integrated in ways that make it convenient.

When using the JQt interface, the File | New temp menu command creates a new script file and a window for editing it. When you choose it, you will see that your J session has both an Term window and a new Edit window to edit the .ijs file.

The Edit window has the name of the .ijs file in its titlebar. Pressing the Enter key in an Edit window does not execute the line, it just moves to the start of a new line.

Type your centigrade definition into the Edit window.

centigrade =: 3 : 0
t1 =. y - 32
t2 =. t1 * 5
t3 =. t2 % 9
)

Be sure to use =: instead of =. in the first line. The =: makes a global definition. If you use =. it is a local definition. This important difference is explained in the next couple of pages. For now, just make sure that the file definitions are made with =: if you want the assignment to have the expected effect.

So far you have just edited changes into the window. The file has not been changed and the verb is still not defined. You have to run the script in order to execute the sentences.

Do menu command Run | Load Script in the Edit window to save the changes to the file and then execute each of the sentences in the file. This is similar to your typing the contents of the file into the Term window, except the sentences and results are not displayed. The only display in the Term window is the system generated sentence that causes the file sentences to be executed. This sentence will be something like: load'c:/j9.4-user/temp/1.ijs' depending on the version of J that you are running. If an error is reported (output in the Term window with a vertical bar on the left) then you have a typo in your script. Correct the text in the Edit window and run it again.

The sentences in the script file have been executed and centigrade is now defined. In the Term window try using centigrade.

   centigrade 32
0

The file created with File | New temp is in the J temp directory and has a temporary format name (a number with an .ijs suffix). However, it would be better to resave it now with a more appropriate name in the J user directory. Use File | Save As..., go up one directory level and then to the the user directory, and set the file name as cf.ijs. The file name in the Edit window titlebar will change to the new name.

Close the cf.ijs window and erase your centigrade definition. You erase the definition of a name by using the utility verb erase with an argument that is the string of the name you want to erase. The result of 1 indicates the erase was successful.

   erase 'centigrade'
1
   centigrade 212
|value error: centigrade
|       centigrade 212

Use File | Recent to open the cf.ijs window and use Run | Load Script to load and run the script to define centigrade.

   centigrade 212
100

Let's add a definition for fahrenheit to the cf.ijs window. Type in the following after your centigrade definition. Again, be sure to use =: .

fahrenheit =: 3 : 0
t1 =. y * 9
t2 =. t1 % 5
t3 =. t2 + 32
)

Use Run | Load Script to load and run the sentences in the cf.ijs script.

   fahrenheit 0
32
   fahrenheit 451
843.8

Close J and restart it.

   centigrade
|value error: centigrade
   fahrenheit
|value error: fahrenheit

Use File | Recent and select your cf.ijs file. Then Run | Load Script and a line similar to

   load'c:/j9.4-user/cf.ijs'

appears in the Term window to run the sentences in the file and your verbs are now defined.

   centigrade 32
0
   fahrenheit 100
212

The line that starts with load that appears in the Term window is in fact the sentence that causes the sentences in the file to be executed. The menu command is just a short cut way of executing this sentence. The string is the full path name to the file to run. You can shorten this full path name to a relative path name when you type it manually.

To check this, close J, restart it, and verify that centigrade is undefined. In the Term window execute the following sentence (your exact user file will depend on the version of J you are running).

   load'~/j9.4-user/cf.ijs'

Now check that your verbs are defined.

Use File | Recent to open your cf.ijs file for editing.

What if there is an error in the script? Let's add an intentional error to the script to see what happens. Add the line foo 123 at the end of the script and run the script again.

   load'c:/j9.4-user/cf.ijs'
|value error: foo
|       foo 123
|[-13]

An error is reported and the execution of the sentences in the script stops. The number at the end of the error report is the line number in the script that had the error.

Remove the error from the script and run it again.

<=   =>

Primer Index               Hover to reveal titles   -   Click to access   -   Current page is highlighted
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
45 46 47 48
50 51 52 53 54 55 56 57
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
97 98 99 100 101 102 103 104 105 106