Print values to stdout using J programming language - j

On this page of the J documentation, there is a command to print values to stdout and the display: http://www.jsoftware.com/help/dictionary/dx001.htm
Specifically:
x 1!:2 y Write.
This seems to suggest it would be possible to call this with the value of y of 2 for screen output, and 4 for stdout, like so:
'123' 1!:2 2
However, when I run this code, I only get the error: "rank error". So my question is, how to use the 1!:2 primitive to print out data values to the screen or stdout?
I notice from another example in the verb echo, this appears to work, but why would I need to bond the 4 to the 1!:2 in order to make it work?
echo '123'
(1!:2&4) '123'
Thanks!

J reads from right to left so it is treating the right argument of !: as 2 2 which is why you are getting the rank error.
'123' 1!:2 2
|rank error
| '123'1 !:2 2
You can fix this by isolating the 2 that you would like to be the right argument of the 1!:2 foreign conjunction.
'123' 1!:2 (2) NB. Parenthesis to isolate the argument
123
123
'123' 1!:2 [ 2 NB. verb [ sends the 2 through to 1!:2
123
123
'123' (1!:2) 2 NB. or you can isolate the conjunction
123
123

Related

Print out even numbers between two given integers

How would I print out even numbers between two numbers?
I have a script where a user enters in two values and them two values are placed into their respective array elements. How would I print the even numbers between the two values?
See man seq. You can use
seq first incr last
for example
seq 4 2 18
to print even numbers from 4 to 18 (inclusive)
If you have bash.
printf '%s\n' {4..18..2}
Or a c-style for loop
for
for ((i=4;i<=18;i+=2)); do echo "$i"; done

How do I separate a user input integer into spaced out digits in python?

user1 = int(input(" Enter a 5 digit number.\n"))
How do i get the output printed in individual digits separated with blank spaces?
You can make use of the fact that print separates its arguments with a space, and use a starred expression to pass all digits of the number to print:
user1 = input("Enter a 5 digit number.\n") # no int(...) here
print(*user1) # if you use int(...) with the input, you should use *str(user1) here
Input:
16532
Output:
1 6 5 3 2
Try this...
' '.join(list(str(user1))

awk key-value issue for array

I meet a awk array issue, details as below:
[~/temp]$ cat test.txt
1
2
3
4
1
2
3
Then I want to count the frequency of the number.
[~/temp]$ awk 'num[$1]++;END{for (i in num){printf("%s\t%-s\n", num[i],i)|"sort -r -n -k1"} }' test.txt
1
2
3
2 3
2 2
2 1
1 4
As you see, why does the output of first 3 line '1 2 3' will come blank value?
Thank for your answer.
An awk statement consists of a pattern and related action. Omitted pattern matches every record of input. Omitted action is an alias to {print $0}, ie. output the current record, which is what you are getting. Looking at the first part of your program:
$ awk 'num[$1]++' file
1
2
3
Let's change that a bit to understand what happens there:
$ awk '{print "NR:",NR,"num["$1"]++:",num[$1]++}' file
NR: 1 num[1]++: 0
NR: 2 num[2]++: 0
NR: 3 num[3]++: 0
NR: 4 num[4]++: 0
NR: 5 num[1]++: 1
NR: 6 num[2]++: 1
NR: 7 num[3]++: 1
Since you are using postfix operator num[$1]++ in the pattern, on records 1-4 it gets evaluated to 0 before it's value is incremented. Output would be different if you used the prefix operator ++num[$1] which would first increment the value of the variable after which it would get evaluated and would lead to outputing every record of input, not just the last three, which you were getting.
Correct way would've been to use num[$1]++ as an action, not as a pattern:
$ awk '{num[$1]++}' file
Put your "per line" part in {} i.e. { num[$1]++; }
awk programs a a collection of [pattern] { actions } (the pattern is optional, the {} is not). Seems that in your case your line is being treated as the pattern.

Making paste command read from standard input

$paste num let
1 a
2 b
3 c
4 d
So when I do
$ cat num | paste - -
1 2
3 4
My question is why doesn't "cat num | paste - -" generate the output as:
1 1
2 2
3 3
4 4
Clearly, paste reads a line from the first 'file' (which is standard input), and then a line from the second 'file' (which is also standard input) and pastes them to create the first line of output. Then it repeats.
The POSIX specification for paste covers the point explicitly:
If '-' is specified for one or more of the files, the standard input shall be used; the standard input shall be read one line at a time, circularly, for each instance of '-'.
You could use "paste num num" to generate
1 1
2 2
3 3
4 4
t1=$(cat 1.txt)
t2=$(cat 2.txt)
echo "$t1" | paste - <(echo "$t2")
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
:)

What is the difference between :g and :%s commands in vim

today I started to use vim. I get confused at :g and :%s commands. So, what is the difference between :g or :%s commands?
:g, short for global, executes a command on all lines that match a regex:
:g/LinesThatMatchThisRegex/ExecuteThisCommand
Example:
:g/hello/d
This will delete (d) all lines that contain hello.
On the other hand, :%s just performs a search (on a regex) and replace throughout the file:
:%s/hello/world/g
The g at the end means global or greedy (this is disputed) so it will replace all occurrences on the line, not just one per line. You can also use the c flag (:%s/hello/world/gc) if you want to confirm each replacement manually.
This command replaces all occurrences of hello with world.
Both the :g and :%s commands support regular expressions.
The s command means substitute and the % means throughout the buffer. So %s means substitute throughout the entire buffer. You can also give a line range:
:10,15s/hello/world/g
This will execute the search and replace seen earlier on only lines 10 to 15 (inclusive).
They are different.
:g can execute commands for matched lines. :s is one of those commands. That is you can combine :g and s
:%s just do search and replace on whole buffer, even though it can do some other things with expression too, but it is not as straightforward as :g.
E.g.:
:g/foo/s/bar/blah/g
this will do bar->blah substitution on lines which contain foo. With :s we could:
:%s/foo/\=substitute(getline('.'), 'bar','blah','g')
so :g is easier.
So if you are dealing with substitution task, usually :s should come up first. If you want to do something like for all lines that matches xxx, I want to delete/join/indent/....... :g maybe helpful for you.
Review:
The ":" mode (e.g. ex-mode) commands in vi or vim have this form:
[Address-specifier] [command] [command-specifics] [cmd-modifiers]
Address can be a single line address (ex-mode operates on "lines"), or a line range.
For instance, a very simple command in "p" which will print the addressed line(s).
:1p - will print line 1.
:5p - will print line 5.
:1,5p - will print lines 1 through 5. 1,5 is an address range.
:7,+3p - will print lines 7 through 10 (7,7+3=10). A relative range.
There are some shorthands in the address space. $, and % are the most popular.
$ means "last line in the file". Thus the expression:
1,$p - will print all lines, 1 to the LAST-line in the file.
The expression 1,$ is so frequently used (e.g. apply the following command to all lines in the file) that it has an even shorter, shorthand, %. % means "1,$"
So:
%p - will print all lines, 1 to the LAST-line in the file, just like 1,$
There is also a special "global" command, whose effect is to supply a set of address prefixing, that is not necessarily a linear range of lines, but is instead determined by a a regular expression match. The ":g/regex/" prefix fits into the "Address specifier" part of the ex-command format (not the command part, which follows it).
It allows specifying a "list" of lines, matched by regular expression rather than "line number", or "range of lines". The matching applies by the regular expression showing up in the line, and then that line is include in the list of lines to which the command will apply.
Application of :1,$s vs %s vs :g/./s
Using the following file as an example:
1: 1
2: 1 2 3 4 5 6 1 2
3: 3 2 1
4: 2 3 1 2
This command, using the global prefix/regex for address, and the "p" print command:
:g/1 2/p - will print
2: 1 2 3 4 5 6 1 2
4: 2 3 1 2
Line 2, and 4 both matched the :g/1 2/ regular expression, and expands effectively into a list of line numbers, with the following command applied to each item in the list. Approximately like this command).
:2p 4p
The substitution command allows substituting a field matching a regular expression, with other text. If we applied the substitution command to our example file, on line 2, we can see its effect.
1: ....
2: 1 2 3 4 5 6 1 2
3: ....
Command:
:2s/1 2/2 1/ will change line 2 to be instead: 2: 2 1 3 4 5 6 1 2
It changes ONLY the first instance of the pattern "1 2" to be "2 1".
If we "undo" this command using "u", we can then run the command again, modified.
We can use the "p" modifier on the command, which for "substitute" does not do much.
It applies the change, but also prints the applied changes at the bottom of the screen (somewhat redundantly in this example).
:2s/1 2/2 1/p
u (to undo), and then we try it again.
We can use the "c" modifier to ask for confirmation.
:2s/1 2/2 1/c The "confirm" modifier for the substitute asks for confirmation on each change.
u (to undo).
The "global" modifier. (Not the global address/regex address operator) can make the substitute command perform multiple substitutions on a line.
:2s/1 2/2 1/g - The "g" here is a modifier to the "s" substitute command.
It means perform the the substitution globally on THE LINE. Modifiers modify "commands", and commands apply to 1 or more lines, as set the address field. The "g" applied to the end of the substitute command means: substitute globally on this line, e.g. every time the regular expression of the substitute command occurs, perform the substitute.
2: 2 1 3 4 5 6 2 1 - Here, both the first and second instance are substituted.
If the substitute command cannot find its match regular expression, then it does nothing. This means it can be applied to a range of lines, and have impacts only on the lines that have at least one match to the substitute command regex.
1,4 s/1 2/2 1/
1,$ s/1 2/2 1/
%s/1 2/2 1/
are all equivalent, and will substitute the FIRST occurrence of the
substitute commands regex match pattern, with the substitute pattern.
1: 1
2: 1 2 3 4 5 6 1 2
3: 3 2 1
4: 2 3 1 2
becomes:
1: 1
2: 2 1 3 4 5 6 1 2
3: 3 2 1
4: 2 3 2 1
Adding "g" to the end gives:
:%s/1 2/2 1/g
1: 1
2: 2 1 3 4 5 6 2 1
3: 3 2 1
4: 2 3 2 1
The g:/regex Prefix
The :g/regex/ Address specifier applies to any command that follows it, and that command can include the substitute command, including with the "g" modifier.
:g/3 4/s/1 2/2 1/g
This command says, "globally match lines with regex /3 4/" and then run command
:s/1 2/2 1/g.
Only line 2 includes the regex /3 4/, so only line 2 is matched. Thus on this file:
:g/3 4/s/1 2/2 1/g is equivalent to:
:2s/1 2/3 4/g, which substitutes all occurrences of 1 2 with 2 1.
1: 1
2: 1 2 3 4 5 6 1 2
3: 3 2 1
4: 2 3 1 2
becomes:
1: 1
2: 2 1 3 4 5 6 2 1
3: 3 2 1
4: 2 3 1 2
Notices that line 4: is unchanged, because it did not have the pattern "3 4" for the Address specifier line match.
:g/regex-line-match/s/match-regex-substitute/sub-pattern/g
:%s/match-regex-substitute/sub-pattern/g
The two lines often can be equivalent in EFFECT. They can often not be equivalent. The equivalence depends on the regex patterns and their matching, and because "substitute" does nothing when a line has no matching match-regex-substitute match pattern.
% = 1,$ which matches all lines, and then applies the substitute pattern.
:g/./ would match every line, if prefixed.
The regex pattern of the "global/regex" prefix if the same as the match-pattern of the substitute would be a lot of extra typing, but would restrict the substitute command to only lines that matched the global/regex. If the global/regex expression truly match every line, such as :g/^.$/, then the global line would have the same effect as %. (Since % would match all lines, and since :g/^.$/ would match all lines, then the "s" would do the same thing in base cases. When using a more typical regular express (that matched some specific string), the :g/regex/ prefix would be different than %. The command "s" would only be applied to lines that first matched the g:/refex/ prefix, instead of to all lines 1,$. The substitute would then try and apply its own "per line" match pattern successfully (and substitute), or find no match on the given line and do nothing.
The place where the global/regex prefix is interesting, is when the global/regex prefix regular expression is different than the substitution match regex pattern. In this case, you apply global/regex FIRST (to determine which lines will then be subject to), the substitute "match-replace-regex" pattern in the substitute command (which can be different). As shown in our example above where we used a global/regex prefix of "3 4", and a substitute match-regex-pattern of "1 2", which is applied SECOND.
VERY ADVANCED:
While global/regex essentially builds a list of lines on which to apply commands, the manner in which that list is built is not the same as the 1,$ or other fixed range specifiers are. Fixed specifiers, are computed, "all at once", at the moment the :[address]command is typed. The global/regex command on the other hand, recomputes its line target after each individual application of its subordinate command.
We will use the "join" command to illustrate the difference.
1: 1
2: 1 2 3 4 5 6 1 2
3: 3 2 1
4: 2 3 1 2
If I specify a range of commands to apply the "join" command to, using range syntax, such as: :1,$j (or :%j) would render:
1: 1 1 2 3 4 5 6 1 2 3 2 1 2 3 1 2
This happens happens because 1,$ selects lines 1,4 at the start, and then applies "j" to every line selected, combining all of the lines of the range.)
But if we instead used the global prefix operator (matching all lines), the application is different:
:g/./j
This will render:
1: 1 1 2 3 4 5 6 1 2
2: 3 2 1 2 3 1 2
The difference occurs because of "how" and "when" the command is applied in each of the two syntax. In the first :%j syntax, all the lines are computed up front, and then "j" is applied to each of those lines.
With the global/regex syntax, the lines and commands are applied on an "as you go", and "from where you are" basis, after EACH application of the command. So the :g/./j command will match LINE1 first, and then runs "j" combining lines 1+2= new-1. It then advances to the "next" line in the file (the new file, new-2), matches that line (/./ matches all) and applies "j" to new-2 (original line3), and new-3 (original 4) to create new-new-2 = 3+4. And then advances to the next line in the "new new file" which is line 3 (but there is no new-new-3, so it stops.) The result is:
1: 1 1 2 3 4 5 6 1 2
2: 3 2 1 2 3 1 2
The key difference is that after application of an instance of the command, the global regex search resumes on the "next" line of the file in existence after the application of the command.
As an earlier poster summed up in far fewer words (but assuming much more knowledge in the reader):
:g/first-search-pattern/s/match-pattern/substitute-pattern/g or /gc for confirm.
SUMMARY:
All of these patterns can be different, the trailing g or gc can be present (all occurences on each line, with or without confirm), or ommitted, (first occurence on each line only). While writing:
:%s/pattern/replace/g is common, the following is nearly equivalent:
:g/./s/pattern/replace/g (less common, but basically the with "substitute" command).

Resources