Input/Output for lines of text: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added zkl)
(J entry)
Line 79: Line 79:
}
}
}</lang>
}</lang>

=={{header|J}}==

Example in bash. jconsole is on the PATH.
<lang J>
$ cat <<EOF | jconsole -js '2!:55@:0:@:(; (1!:2) 4:)@:(}. {.~ _ ". [: }: 0&{::)@:(<;.2)@:(1!:1) 3'
> 3
> hello
> hello world
> Pack my Box with 5 dozen liquor jugs
> EOF
hello
hello world
Pack my Box with 5 dozen liquor jugs
</lang>
From the dictionary of j (DOJ) the data flow for the fork (f g h) is

''5. Forks

As illustrated above, an isolated sequence of three verbs is called a fork; its monadic and dyadic cases are defined by:''
<pre>
g
/ \
f h
| |
y y

g
/ \
f h
/ \ / \
x y x y
</pre>

Reading from left to right

2!:55 is exit.
0: is a verb that returns 0 for any input.
So now we know the script will terminate the j session with successful status.

What does it do before this?
1!:2 is "write to file", with left argument x as the data to write, and the right argument y specifies the file.
4: is a verb returning 4 for any input. File 4 is stdout.
; is "raze"
The fork (; (1!:2) 4:) writes some data to stdout.
Good!

What is the data?
(}. {.~ _ ". [: }: 0&{::)
Because it has an odd number of verbs, this expresses a fork. And because {:: (fetch) is in the fork the right argument y is a vector of boxes. We know that the data has a number followed by some lines of text. Let's read the fork from left to right. The second verb, {. is "take" modified by the ~ "passive" adverb to swap arguments. Take uses a shape argument on left (x), and the data to take as y. Remembering the passive part, the data to which take applies will be the beheaded vector of boxes---that removes the first line which is the number, and the fork to the right of {.~ computes the shape. Now looking at the fourth verb, ". (numbers) the default in case of error is _ (infinity meaning "all" when used along a shape dimension to take) and the data for numbers is the curtailed }: content of the first box (index origin 0). 0 is & (bonded also known as curried) to fetch. Curtailing removes the line feed. Since this gives a list of boxes, but we need to display literal data, raze "unboxes" one level of boxing.
Good, if we have a list of boxed lines of input.

(<;.2)@:(1!:1) 3
(<;.2) is "< (box) ;. (cut) 2 . The 2 specifies the last item of the data as the fret, and to preserve the frets.
(1!:1) 3 is "read stdin".

I chose to connect the parts into a single verb using @: (at).


With predefined verbs from standard profile we can write the simpler and slightly more robust sentence which ensures a final linefeed fret and discards the frets with <;._2

exit@:0:@:(smoutput&>)@:(}. {.~ _ ". 0&{::)@:cutLF@:(1!:1) 3

Cheers! That's tacit j.


=={{header|Perl 6}}==
=={{header|Perl 6}}==

Revision as of 02:28, 7 June 2014

Input/Output for lines of text is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

The first line contains the number of lines to follow, followed by that number of lines of text on STDIN. Output to STDOUT each line of input by passing it to a method as an intermediate step. The code should demonstrate these 3 things. See also Input/Output for Pairs of Numbers and File/Input and Output.

Sample input with corresponding output

Input

3
hello
hello world
Pack my Box with 5 dozen liquor jugs 

Output

hello
hello world
Pack my Box with 5 dozen liquor jugs

C

<lang C> /*Abhishek Ghosh, 20th March 2014, Rotterdam*/

  1. include<stdlib.h>
  2. include<stdio.h>
  1. define LEN 100 /* Max string length */

int main() { char **list; int num, i;

scanf("%d",&num);

list = (char**)malloc(num*sizeof(char*));

for(i=0;i<num;i++) { list[i] = (char*)malloc(LEN*sizeof(char)); fflush(stdin); fgets(list[i],LEN,stdin); }

printf("\n");

for(i=0;i<num;i++) { printf("%s",list[i]); }

return 0; } </lang>

D

<lang d>void main() {

   import std.stdio, std.conv, std.string;
   enum doStuff = (in string line) => line.write;
   foreach (_; 0 .. readln.strip.to!uint)
       doStuff(readln.idup);

}</lang>

Java

<lang java>import java.util.Scanner;

public class Main { public static void doStuff(String word){ System.out.println(word); }

public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = Integer.parseInt(in.nextLine()); //doesn't use nextInt() so nextLine doesn't just read newline character for(int i=0; i<n; i++){ String word = in.nextLine(); doStuff(word); } } }</lang>

J

Example in bash. jconsole is on the PATH. <lang J> $ cat <<EOF | jconsole -js '2!:55@:0:@:(; (1!:2) 4:)@:(}. {.~ _ ". [: }: 0&{::)@:(<;.2)@:(1!:1) 3' > 3 > hello > hello world > Pack my Box with 5 dozen liquor jugs > EOF hello hello world Pack my Box with 5 dozen liquor jugs </lang> From the dictionary of j (DOJ) the data flow for the fork (f g h) is

5. Forks

As illustrated above, an isolated sequence of three verbs is called a fork; its monadic and dyadic cases are defined by:

  g
 / \
f   h
|   |
y   y

    g
   / \
  f   h
 / \ / \
x  y x  y

Reading from left to right

2!:55 is exit. 0: is a verb that returns 0 for any input. So now we know the script will terminate the j session with successful status.

What does it do before this? 1!:2 is "write to file", with left argument x as the data to write, and the right argument y specifies the file. 4: is a verb returning 4 for any input. File 4 is stdout.

is "raze"

The fork (; (1!:2) 4:) writes some data to stdout. Good!

What is the data? (}. {.~ _ ". [: }: 0&{::) Because it has an odd number of verbs, this expresses a fork. And because {:: (fetch) is in the fork the right argument y is a vector of boxes. We know that the data has a number followed by some lines of text. Let's read the fork from left to right. The second verb, {. is "take" modified by the ~ "passive" adverb to swap arguments. Take uses a shape argument on left (x), and the data to take as y. Remembering the passive part, the data to which take applies will be the beheaded vector of boxes---that removes the first line which is the number, and the fork to the right of {.~ computes the shape. Now looking at the fourth verb, ". (numbers) the default in case of error is _ (infinity meaning "all" when used along a shape dimension to take) and the data for numbers is the curtailed }: content of the first box (index origin 0). 0 is & (bonded also known as curried) to fetch. Curtailing removes the line feed. Since this gives a list of boxes, but we need to display literal data, raze "unboxes" one level of boxing. Good, if we have a list of boxed lines of input.

(<;.2)@:(1!:1) 3 (<;.2) is "< (box) ;. (cut) 2 . The 2 specifies the last item of the data as the fret, and to preserve the frets. (1!:1) 3 is "read stdin".

I chose to connect the parts into a single verb using @: (at).


With predefined verbs from standard profile we can write the simpler and slightly more robust sentence which ensures a final linefeed fret and discards the frets with <;._2

exit@:0:@:(smoutput&>)@:(}. {.~ _ ". 0&{::)@:cutLF@:(1!:1) 3

Cheers! That's tacit j.

Perl 6

<lang perl6>$*OUT.say($*IN.get) xx $*IN.get;</lang>

Python

<lang python>try: input = raw_input except: pass

def do_stuff(words): print(words)

linecount = int(input()) for x in range(linecount): line = input() do_stuff(line)</lang>

Racket

Translation of: Python

<lang Racket>#lang racket (define (do-stuff str)

 (displayln str))
(define line-count (read)) ;reads all kind of things

(define line-count (string->number ;only reads numbers

                   (string-trim
                    (read-line)))) 

(for ([i (in-range line-count)])

 (do-stuff (read-line)))</lang>

REXX

Programming note: this method was chosen because the standard input may be identical to the standard output. <lang rexx>/*REXX program outputs a number of lines from the default input file. */

  1. =linein() /*the number of lines to follow. */
 do j=1  for #;   x.j=linein();  end  /*obtain input lines from stdin. */

call stuff /*call STUFF routine for output. */ exit /*stick a fork in it, we're done.*/ /*──────────────────────────────────STUFF routine───────────────────────*/ stuff: do k=1 for #; call lineout ,x.k; end; return /*output.*/</lang>

Ruby

<lang ruby>def do_stuff(word) puts word end

t = gets.to_i for i in 1..t do word = gets do_stuff(word) end</lang>

Tcl

<lang tcl>proc do_stuff {line} {

   puts $line

}

foreach - [lrepeat [gets stdin] dummy] {

   do_stuff [gets stdin]

}</lang>

zkl

File ff.zkl: <lang zkl>numLines:=File.stdin.readln().strip().toInt(); text:=File.stdin.readln(numLines);

text.apply(File.stdout.write);</lang>

Output:
cat foo.txt | zkl ff
hello
hello world
Pack my Box with 5 dozen liquor jugs