Input/Output for lines of text: Difference between revisions
m
→{{header|Wren}}: Changed to Wren S/H
m (→{{header|C}}) |
m (→{{header|Wren}}: Changed to Wren S/H) |
||
(7 intermediate revisions by 7 users not shown) | |||
Line 34:
{{trans|Python}}
<
print(words)
Line 40:
L 1..linecount
V line = input()
do_stuff(line)</
=={{header|Action!}}==
The user must type in the monitor the following command after compilation and before running the program!<pre>SET EndProg=*</pre>
{{libheader|Action! Tool Kit}}
<syntaxhighlight lang="action!">CARD EndProg ;required for ALLOCATE.ACT
INCLUDE "D2:ALLOCATE.ACT" ;from the Action! Tool Kit. You must type 'SET EndProg=*' from the monitor after compiling, but before running this program!
PROC Main()
DEFINE PTR="CARD"
BYTE i,nLines
PTR ARRAY lines(256)
CHAR ARRAY line(256),p
AllocInit(0)
Put(125) PutE()
nLines=InputB()
IF nLines=0 THEN RETURN FI
FOR i=0 TO nLines-1
DO
InputS(line)
p=Alloc(line(0)+1)
MoveBlock(p,line,line(0)+1)
lines(i)=p
OD
PutE()
FOR i=0 TO nLines-1
DO
p=lines(i)
PrintE(p)
OD
FOR i=0 TO nLines-1
DO
p=lines(i)
Free(p,p(0)+1)
lines(i)=0
OD
nLines=0
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Input_Output_for_lines_of_text.png Screenshot from Atari 8-bit computer]
<pre>
3
hello
hello world
Pack my Box with 5 dozen liquor jugs
hello
hello world
Pack my Box with 5 dozen liquor jugs
</pre>
=={{header|Ada}}==
<syntaxhighlight lang="ada">--
-- The first line contains the number of lines to follow, followed by that
-- number of lines of text on STDIN.
Line 63 ⟶ 118:
end loop;
end Main;
</syntaxhighlight>
'''Input:'''
<pre>
Line 80 ⟶ 135:
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}}
<
PROC show line = ( STRING line )VOID:
print( ( line, newline ) );
Line 91 ⟶ 146:
show line( ( STRING line; read( ( line, newline ) ); line ) )
OD
</syntaxhighlight>
=={{header|ALGOL W}}==
<
% outputs line on a newline %
procedure showLine ( string(80) value line ); write( line );
Line 105 ⟶ 160:
showLine( line )
end for_lineNumber
end.</
=={{header|Applesoft BASIC}}==
<syntaxhighlight lang="gwbasic"> 100 GOSUB 230"INPUT LINE"
110 LET N = VAL (L$) - 1
120 IF N < 0 THEN END
130 DIM L$(N)
140 FOR I = 0 TO N
150 GOSUB 230"INPUT LINE"
160 LET L$(I) = L$
170 NEXT I
190 FOR I = 0 TO N
200 PRINT L$(I)
210 NEXT
220 END
230 LET L$ = ""
240 LET C$ = ""
250 FOR C = 0 TO 1 STEP 0
260 LET L$ = L$ + C$
270 GET C$
280 PRINT CHR$ (0)C$;
290 LET C = C$ = CHR$ (13)
300 NEXT C
310 LET C = FRE (0)
320 RETURN</syntaxhighlight>
'''Input'''
<pre>
3
hello
hello world
Pack my Box with 5 dozen liquor jugs
</pre>
{{out}}
<pre>
hello
hello world
Pack my Box with 5 dozen liquor jugs
</pre>
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">printLine: function [line]-> print line
lineCount: to :integer strip input ""
do.times:lineCount [
line: input ""
printLine line
]</syntaxhighlight>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f INPUT_OUTPUT_FOR_LINES_OF_TEXT.AWK
BEGIN {
Line 119 ⟶ 221:
exit(0)
}
</syntaxhighlight>
=={{header|Batch File}}==
<
@echo off
setlocal enabledelayedexpansion
Line 132 ⟶ 234:
for /l %%i in (1,1,%lines%) do echo !line%%i!
pause>nul
</syntaxhighlight>
{{in}}
<pre>
Line 148 ⟶ 250:
=={{header|C}}==
<syntaxhighlight lang="c">
#include<stdlib.h>
#include<stdio.h>
Line 179 ⟶ 281:
return 0;
}
</syntaxhighlight>
Alternative code:
Line 191 ⟶ 293:
When the total number of lines entered has been reached, it will display a message indicating that you must enter a number.
<syntaxhighlight lang="c">
// Programa IO.C
#include <stdio.h>
Line 241 ⟶ 343:
return 0;
}
</syntaxhighlight>
{{out}}
<pre>
Line 261 ⟶ 363:
=={{header|C++}}==
<
#include <vector>
Line 283 ⟶ 385:
std::cout << value << "\n";
}
}</
'''Input:'''
<pre>
Line 299 ⟶ 401:
=={{header|D}}==
<
import std.stdio, std.conv, std.string;
Line 306 ⟶ 408:
foreach (_; 0 .. readln.strip.to!uint)
doStuff(readln.idup);
}</
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
<syntaxhighlight lang="delphi">
program Output_for_Lines_of_Text;
Line 357 ⟶ 459:
Writeln(lines);
Readln;
end.</
{{out}}
<pre>
Line 371 ⟶ 473:
=={{header|Factor}}==
<
IN: input-output
Line 377 ⟶ 479:
M: string do-stuff print ;
readln drop [ do-stuff ] each-line</
=={{header|Free Pascal}}==
This requires FPC – the FreePascal compiler – to be in a configuration enabling the use ob <tt>object</tt>s.
<
type
Line 405 ⟶ 507:
obj.method(line);
end;
end.</
=={{header|FreeBASIC}}==
<
Sub printLines(lines() As String)
Line 424 ⟶ 526:
Print
printLines lines()
Sleep</
{{out}}
Line 439 ⟶ 541:
=={{header|Go}}==
<
import (
Line 490 ⟶ 592:
func doStuff(line string) {
fmt.Println(line)
}</
=={{header|Haskell}}==
<
main = do
number <- getLine
input <- replicateM (read number) getLine
mapM_ putStrLn input
</
<pre>
3
Line 516 ⟶ 618:
Example in bash. jconsole is on the PATH.
<syntaxhighlight lang="j">
$ cat <<EOF | jconsole -js '2!:55@:0:@:(; (1!:2) 4:)@:(}. {.~ _ ". [: }: 0&{::)@:(<;.2)@:(1!:1) 3'
> 3
Line 526 ⟶ 628:
hello world
Pack my Box with 5 dozen liquor jugs
</syntaxhighlight>
From the dictionary of j (DOJ) the data flow for the fork (f g h) is
Line 578 ⟶ 680:
=={{header|Java}}==
<
public class Main {
Line 593 ⟶ 695:
}
}
}</
=={{header|jq}}==
The following works for both the C and the Go implementations of jq.
<pre>
jq -Rr 'limit(tonumber; inputs)'
</pre>
=={{header|Julia}}==
{{works with|Julia|0.6}}
<
print(words)
end
Line 606 ⟶ 714:
words = readline()
dosomething(words)
end</
=={{header|Kotlin}}==
<
fun output(lines: Array<String>) = println(lines.joinToString("\n"))
Line 619 ⟶ 727:
println("\nThe lines you entered are:\n")
output(lines)
}</
{{out}}
Line 638 ⟶ 746:
=={{header|Lua}}==
<
for _, line in pairs(t) do print(line) end
end
Line 644 ⟶ 752:
local lineTable, numLines = {}, io.read()
for i = 1, numLines do table.insert(lineTable, io.read()) end
show(lineTable)</
=={{header|Nanoquery}}==
{{trans|Ursa}}
<
amount = int(input())
Line 661 ⟶ 769:
for line in lines
println line
end</
{{out}}
<pre>3
Line 673 ⟶ 781:
=={{header|Nim}}==
<
proc write(line: string) =
Line 681 ⟶ 789:
for _ in 1..lineCount:
let line = stdin.readLine()
line.write()</
=={{header|Objeck}}==
<
class Rosetta {
Line 703 ⟶ 811:
};
}
}</
=={{header|PARI/GP}}==
This task is not possible to implement directly in GP: for <code>input()</code> to take a string the user would have to wrap it in quotes (and escape quotes and newlines). One must use PARI:
<
#include <stdlib.h>
#include <pari/pari.h>
Line 733 ⟶ 841:
pari_printf("%Ps", vec);
return 0;
}</
=={{header|Perl}}==
<
do_stuff(scalar <>) for 1..$n;
sub do_stuff { print $_[0] }</
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(notonline)-->
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (file i/o)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">stack</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">procedure</span> <span style="color: #7060A8;">push</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">line</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">stack</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">stack</span><span style="color: #0000FF;">,</span><span style="color: #000000;">line</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">pop_all</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">while</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">stack</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">stack</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span>
<span style="color: #000000;">stack</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">stack</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..$]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">line</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">gets</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">scanf</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">trim</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"input not a number\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">abort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">line</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">gets</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">push</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"===\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">pop_all</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
{{out}}
(or more accurately the final state of the console)
Line 783 ⟶ 894:
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
# script.ps1
Line 790 ⟶ 901:
# ./script file.txt
</syntaxhighlight>
=={{header|Prolog}}==
<syntaxhighlight lang="prolog">
number_of_lines(Num) :-
current_input(In),
Line 814 ⟶ 925:
number_of_lines(Num),
input_lines_for_num(Num, []).
</syntaxhighlight>
{{out}}
<pre>
Line 835 ⟶ 946:
=={{header|Python}}==
<
except: pass
Line 844 ⟶ 955:
for x in range(linecount):
line = input()
do_stuff(line)</
=={{header|Racket}}==
{{trans|Python}}
<
(define (do-stuff str)
(displayln str))
Line 859 ⟶ 970:
(for ([i (in-range line-count)])
(do-stuff (read-line)))</
=={{header|Raku}}==
Line 866 ⟶ 977:
Short version:
<syntaxhighlight lang="raku"
Verbose version:
<syntaxhighlight lang="raku"
say $line;
}
Line 878 ⟶ 989:
my $line = get;
do-stuff $line;
}</
=={{header|REXX}}==
Programming note: this method was chosen because the standard input may be identical to the standard output.
<
#=linein() /*number of lines to be read from C.L. */
Line 890 ⟶ 1,001:
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
stuff: do k=1 for #; call lineout ,x.k; end; return</
{{out|output|text= where showing the input and the output to the terminal:}}
<pre>
Line 903 ⟶ 1,014:
=={{header|Ring}}==
<
# Project : Input/Output for Lines of Text
Line 920 ⟶ 1,031:
see lines[i] + nl
next
</syntaxhighlight>
Input:
<pre>
Line 936 ⟶ 1,047:
=={{header|Ruby}}==
<
puts line
end
Line 944 ⟶ 1,055:
line = gets
do_stuff(line)
end</
=={{header|Scala}}==
<
object IOLines extends App {
private val in = scala.io.StdIn
Line 958 ⟶ 1,069:
doStuff(word)
}
}</
=={{header|Tcl}}==
<
puts $line
}
Line 967 ⟶ 1,078:
foreach - [lrepeat [gets stdin] dummy] {
do_stuff [gets stdin]
}</
=={{header|Ursa}}==
<
# input/output for lines of text
#
Line 990 ⟶ 1,101:
out lines<i> endl console
end for
</syntaxhighlight>
=={{header|Wren}}==
This assumes that both Stdin and Stdout are connected to a terminal.
<
var output = Fn.new { |lines| System.print(lines.join("\n")) }
Line 1,003 ⟶ 1,114:
for (i in 0...n) lines[i] = Stdin.readLine()
System.print()
output.call(lines)</
{{out}}
Line 1,017 ⟶ 1,128:
Pack my Box with 5 dozen liquor jugs
</pre>
=={{header|XPL0}}==
The input file must be redirected on the command line, for example: iotext <iotext.txt
<syntaxhighlight lang="xpl0">string 0;
proc PrintLn(Str); \"method" to print a line of text
char Str;
[Text(0, Str);
CrLf(0);
];
char Line(1000);
int N, I, C;
for N:= 1 to IntIn(1) do
[I:= 0;
loop [repeat C:= ChIn(1) until C # $0D \CR\;
if C = $0A \LF\ then quit;
Line(I):= C;
I:= I+1;
];
Line(I):= 0;
PrintLn(Line);
]</syntaxhighlight>
=={{header|zkl}}==
File ff.zkl:
<
text:=File.stdin.readln(numLines);
text.apply(File.stdout.write);</
{{out}}
<pre>
|