user3234709
user3234709

Reputation: 405

How to work with command line arguments in J

In J console mode if I type ARGV I get the full path of jqt.exe But when I try to pass some strings to a J script file I get 'syntax error' or 'domain error'. How does argument passing and retrieval or display work?

Upvotes: 1

Views: 224

Answers (2)

Julian Fondren
Julian Fondren

Reputation: 5609

An interaction with a script that just prints out its arguments:

$ cat args.ijs
#! /usr/bin/env j
exit echo each ARGV
$ ./args.ijs
j
./args.ijs
$ ./args.ijs 1 2 3
j
./args.ijs
1
2
3
$ ./args.ijs '1 2' 3
j
./args.ijs
1 2
3

ARGV is a list of the boxed arguments to the script. It works like any list of boxed literals, and if you're a domain error it's from some verb in your script that's given arguments it's not defined to handle. If you're getting a syntax error it's because there's something in your script with incorrect syntax. This is unrelated to ARGV as such.

Perhaps you're expecting numerical arguments to be numbers? Arguments are always delivered as strings. Here's a slightly more involved script with usage, that prints the sum of the factorials of its arguments:

#! /usr/bin/env j
sumfact =: [: +/ [: ! x:
3 : 0''
  if. (#ARGV) > 2 do.
    echo sumfact > 0&". each 2}.ARGV
    exit 0
  else.
    echo 'usage: ', (1{::ARGV), ' <n1> [<n2> ... <nn>]'
    exit 1
  end.
)

As used:

$ ./sumfact.ijs
usage: ./sumfact.ijs <n1> [<n2> ... <nn>]
$ ./sumfact.ijs 0
1
$ ./sumfact.ijs 5
120
$ ./sumfact.ijs 5 5 5
360
$ ./sumfact.ijs 100
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

The text after #! isn't important; I use /usr/bin/env j because I have a j in my path that's the usual bin/jconsole of a J installation.

Upvotes: 1

bob
bob

Reputation: 4302

If you want to write to a file you would pass the information using

'string' 1:!2 'filepath/jscriptfile'

see https://www.jsoftware.com/help/dictionary/dx001.htm

if you want to pass an argument to a verb declared in a script, you would first have to load the script

load 'filepath/jscriptfile'

Then as long as the script contains verbs that have been assigned using =: so that the verb is not local to the script file, you would pass the string to the verb, which has now been loaded.

verb 'string' 

Upvotes: 1

Related Questions