Input/Output for lines of text: Difference between revisions

Content added Content deleted
(Applesoft BASIC)
m (syntax highlighting fixup automation)
Line 34: Line 34:
{{trans|Python}}
{{trans|Python}}


<lang 11l>F do_stuff(words)
<syntaxhighlight lang="11l">F do_stuff(words)
print(words)
print(words)


Line 40: Line 40:
L 1..linecount
L 1..linecount
V line = input()
V line = input()
do_stuff(line)</lang>
do_stuff(line)</syntaxhighlight>


=={{header|Action!}}==
=={{header|Action!}}==
The user must type in the monitor the following command after compilation and before running the program!<pre>SET EndProg=*</pre>
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}}
{{libheader|Action! Tool Kit}}
<lang Action!>CARD EndProg ;required for ALLOCATE.ACT
<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!
INCLUDE "D2:ALLOCATE.ACT" ;from the Action! Tool Kit. You must type 'SET EndProg=*' from the monitor after compiling, but before running this program!
Line 83: Line 83:
OD
OD
nLines=0
nLines=0
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Input_Output_for_lines_of_text.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Input_Output_for_lines_of_text.png Screenshot from Atari 8-bit computer]
Line 98: Line 98:


=={{header|Ada}}==
=={{header|Ada}}==
<syntaxhighlight lang="ada">--
<lang Ada>--
-- The first line contains the number of lines to follow, followed by that
-- The first line contains the number of lines to follow, followed by that
-- number of lines of text on STDIN.
-- number of lines of text on STDIN.
Line 118: Line 118:
end loop;
end loop;
end Main;
end Main;
</syntaxhighlight>
</lang>
'''Input:'''
'''Input:'''
<pre>
<pre>
Line 135: Line 135:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}}
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}}
<lang algol68># outputs line plus a newline #
<syntaxhighlight lang="algol68"># outputs line plus a newline #
PROC show line = ( STRING line )VOID:
PROC show line = ( STRING line )VOID:
print( ( line, newline ) );
print( ( line, newline ) );
Line 146: Line 146:
show line( ( STRING line; read( ( line, newline ) ); line ) )
show line( ( STRING line; read( ( line, newline ) ); line ) )
OD
OD
</syntaxhighlight>
</lang>


=={{header|ALGOL W}}==
=={{header|ALGOL W}}==
<lang algolw>begin
<syntaxhighlight lang="algolw">begin
% outputs line on a newline %
% outputs line on a newline %
procedure showLine ( string(80) value line ); write( line );
procedure showLine ( string(80) value line ); write( line );
Line 160: Line 160:
showLine( line )
showLine( line )
end for_lineNumber
end for_lineNumber
end.</lang>
end.</syntaxhighlight>


=={{header|Applesoft BASIC}}==
=={{header|Applesoft BASIC}}==
<lang gwbasic> 100 GOSUB 230"INPUT LINE"
<syntaxhighlight lang="gwbasic"> 100 GOSUB 230"INPUT LINE"
110 LET N = VAL (L$) - 1
110 LET N = VAL (L$) - 1
120 IF N < 0 THEN END
120 IF N < 0 THEN END
Line 184: Line 184:
300 NEXT C
300 NEXT C
310 LET C = FRE (0)
310 LET C = FRE (0)
320 RETURN</lang>
320 RETURN</syntaxhighlight>
'''Input'''
'''Input'''
<pre>
<pre>
Line 199: Line 199:
</pre>
</pre>
=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f INPUT_OUTPUT_FOR_LINES_OF_TEXT.AWK
# syntax: GAWK -f INPUT_OUTPUT_FOR_LINES_OF_TEXT.AWK
BEGIN {
BEGIN {
Line 210: Line 210:
exit(0)
exit(0)
}
}
</syntaxhighlight>
</lang>


=={{header|Batch File}}==
=={{header|Batch File}}==
<lang dos>
<syntaxhighlight lang="dos">
@echo off
@echo off
setlocal enabledelayedexpansion
setlocal enabledelayedexpansion
Line 223: Line 223:
for /l %%i in (1,1,%lines%) do echo !line%%i!
for /l %%i in (1,1,%lines%) do echo !line%%i!
pause>nul
pause>nul
</syntaxhighlight>
</lang>
{{in}}
{{in}}
<pre>
<pre>
Line 239: Line 239:


=={{header|C}}==
=={{header|C}}==
<syntaxhighlight lang="c">
<lang C>
#include<stdlib.h>
#include<stdlib.h>
#include<stdio.h>
#include<stdio.h>
Line 270: Line 270:
return 0;
return 0;
}
}
</syntaxhighlight>
</lang>


Alternative code:
Alternative code:
Line 282: Line 282:
When the total number of lines entered has been reached, it will display a message indicating that you must enter a number.
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">
<lang C>
// Programa IO.C
// Programa IO.C
#include <stdio.h>
#include <stdio.h>
Line 332: Line 332:
return 0;
return 0;
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 352: Line 352:


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>
#include <vector>
#include <vector>


Line 374: Line 374:
std::cout << value << "\n";
std::cout << value << "\n";
}
}
}</lang>
}</syntaxhighlight>
'''Input:'''
'''Input:'''
<pre>
<pre>
Line 390: Line 390:


=={{header|D}}==
=={{header|D}}==
<lang d>void main() {
<syntaxhighlight lang="d">void main() {
import std.stdio, std.conv, std.string;
import std.stdio, std.conv, std.string;


Line 397: Line 397:
foreach (_; 0 .. readln.strip.to!uint)
foreach (_; 0 .. readln.strip.to!uint)
doStuff(readln.idup);
doStuff(readln.idup);
}</lang>
}</syntaxhighlight>
=={{header|Delphi}}==
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{libheader| System.SysUtils}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Output_for_Lines_of_Text;
program Output_for_Lines_of_Text;


Line 448: Line 448:
Writeln(lines);
Writeln(lines);
Readln;
Readln;
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 462: Line 462:


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>USING: io kernel strings ;
<syntaxhighlight lang="factor">USING: io kernel strings ;
IN: input-output
IN: input-output


Line 468: Line 468:
M: string do-stuff print ;
M: string do-stuff print ;


readln drop [ do-stuff ] each-line</lang>
readln drop [ do-stuff ] each-line</syntaxhighlight>


=={{header|Free Pascal}}==
=={{header|Free Pascal}}==
This requires FPC – the FreePascal compiler – to be in a configuration enabling the use ob <tt>object</tt>s.
This requires FPC – the FreePascal compiler – to be in a configuration enabling the use ob <tt>object</tt>s.
<lang pascal>program head(input, output, stdErr);
<syntaxhighlight lang="pascal">program head(input, output, stdErr);


type
type
Line 496: Line 496:
obj.method(line);
obj.method(line);
end;
end;
end.</lang>
end.</syntaxhighlight>


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>' FB 1.05.0 Win64
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64


Sub printLines(lines() As String)
Sub printLines(lines() As String)
Line 515: Line 515:
Print
Print
printLines lines()
printLines lines()
Sleep</lang>
Sleep</syntaxhighlight>


{{out}}
{{out}}
Line 530: Line 530:


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 581: Line 581:
func doStuff(line string) {
func doStuff(line string) {
fmt.Println(line)
fmt.Println(line)
}</lang>
}</syntaxhighlight>


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang Haskell>import Control.Monad
<syntaxhighlight lang="haskell">import Control.Monad
main = do
main = do
number <- getLine
number <- getLine
input <- replicateM (read number) getLine
input <- replicateM (read number) getLine
mapM_ putStrLn input
mapM_ putStrLn input
</lang>'''Input:'''
</syntaxhighlight>'''Input:'''
<pre>
<pre>
3
3
Line 607: Line 607:


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


Line 669: Line 669:


=={{header|Java}}==
=={{header|Java}}==
<lang java>import java.util.Scanner;
<syntaxhighlight lang="java">import java.util.Scanner;


public class Main {
public class Main {
Line 684: Line 684:
}
}
}
}
}</lang>
}</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
{{works with|Julia|0.6}}
{{works with|Julia|0.6}}


<lang julia>function dosomething(words)
<syntaxhighlight lang="julia">function dosomething(words)
print(words)
print(words)
end
end
Line 697: Line 697:
words = readline()
words = readline()
dosomething(words)
dosomething(words)
end</lang>
end</syntaxhighlight>


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>// version 1.1
<syntaxhighlight lang="scala">// version 1.1


fun output(lines: Array<String>) = println(lines.joinToString("\n"))
fun output(lines: Array<String>) = println(lines.joinToString("\n"))
Line 710: Line 710:
println("\nThe lines you entered are:\n")
println("\nThe lines you entered are:\n")
output(lines)
output(lines)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 729: Line 729:


=={{header|Lua}}==
=={{header|Lua}}==
<lang lua>function show (t)
<syntaxhighlight lang="lua">function show (t)
for _, line in pairs(t) do print(line) end
for _, line in pairs(t) do print(line) end
end
end
Line 735: Line 735:
local lineTable, numLines = {}, io.read()
local lineTable, numLines = {}, io.read()
for i = 1, numLines do table.insert(lineTable, io.read()) end
for i = 1, numLines do table.insert(lineTable, io.read()) end
show(lineTable)</lang>
show(lineTable)</syntaxhighlight>


=={{header|Nanoquery}}==
=={{header|Nanoquery}}==
{{trans|Ursa}}
{{trans|Ursa}}
<lang Nanoquery>// get how many lines the user wants
<syntaxhighlight lang="nanoquery">// get how many lines the user wants
amount = int(input())
amount = int(input())


Line 752: Line 752:
for line in lines
for line in lines
println line
println line
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>3
<pre>3
Line 764: Line 764:


=={{header|Nim}}==
=={{header|Nim}}==
<lang Nim>import strutils
<syntaxhighlight lang="nim">import strutils


proc write(line: string) =
proc write(line: string) =
Line 772: Line 772:
for _ in 1..lineCount:
for _ in 1..lineCount:
let line = stdin.readLine()
let line = stdin.readLine()
line.write()</lang>
line.write()</syntaxhighlight>


=={{header|Objeck}}==
=={{header|Objeck}}==
<lang objeck>use System.IO.File;
<syntaxhighlight lang="objeck">use System.IO.File;


class Rosetta {
class Rosetta {
Line 794: Line 794:
};
};
}
}
}</lang>
}</syntaxhighlight>


=={{header|PARI/GP}}==
=={{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:
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:
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <pari/pari.h>
#include <pari/pari.h>
Line 824: Line 824:
pari_printf("%Ps", vec);
pari_printf("%Ps", vec);
return 0;
return 0;
}</lang>
}</syntaxhighlight>


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>$n = scalar <>;
<syntaxhighlight lang="perl">$n = scalar <>;


do_stuff(scalar <>) for 1..$n;
do_stuff(scalar <>) for 1..$n;


sub do_stuff { print $_[0] }</lang>
sub do_stuff { print $_[0] }</syntaxhighlight>


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(notonline)-->
<!--<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: #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: #004080;">sequence</span> <span style="color: #000000;">stack</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
Line 862: Line 862:
<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: #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>
<span style="color: #000000;">pop_all</span><span style="color: #0000FF;">()</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
(or more accurately the final state of the console)
(or more accurately the final state of the console)
Line 877: Line 877:


=={{header|PowerShell}}==
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
# script.ps1
# script.ps1


Line 884: Line 884:


# ./script file.txt
# ./script file.txt
</syntaxhighlight>
</lang>


=={{header|Prolog}}==
=={{header|Prolog}}==
<syntaxhighlight lang="prolog">
<lang Prolog>
number_of_lines(Num) :-
number_of_lines(Num) :-
current_input(In),
current_input(In),
Line 908: Line 908:
number_of_lines(Num),
number_of_lines(Num),
input_lines_for_num(Num, []).
input_lines_for_num(Num, []).
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 929: Line 929:


=={{header|Python}}==
=={{header|Python}}==
<lang python>try: input = raw_input
<syntaxhighlight lang="python">try: input = raw_input
except: pass
except: pass


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


=={{header|Racket}}==
=={{header|Racket}}==
{{trans|Python}}
{{trans|Python}}
<lang Racket>#lang racket
<syntaxhighlight lang="racket">#lang racket
(define (do-stuff str)
(define (do-stuff str)
(displayln str))
(displayln str))
Line 953: Line 953:


(for ([i (in-range line-count)])
(for ([i (in-range line-count)])
(do-stuff (read-line)))</lang>
(do-stuff (read-line)))</syntaxhighlight>


=={{header|Raku}}==
=={{header|Raku}}==
Line 960: Line 960:
Short version:
Short version:


<lang perl6>say get for ^get;</lang>
<syntaxhighlight lang="raku" line>say get for ^get;</syntaxhighlight>


Verbose version:
Verbose version:


<lang perl6>sub do-stuff ($line) {
<syntaxhighlight lang="raku" line>sub do-stuff ($line) {
say $line;
say $line;
}
}
Line 972: Line 972:
my $line = get;
my $line = get;
do-stuff $line;
do-stuff $line;
}</lang>
}</syntaxhighlight>


=={{header|REXX}}==
=={{header|REXX}}==
Programming note: &nbsp; this method was chosen because the standard input may be identical to the standard output.
Programming note: &nbsp; this method was chosen because the standard input may be identical to the standard output.
<lang rexx>/*REXX program writes a number of lines from the default input file (C.L.). */
<syntaxhighlight lang="rexx">/*REXX program writes a number of lines from the default input file (C.L.). */
#=linein() /*number of lines to be read from C.L. */
#=linein() /*number of lines to be read from C.L. */


Line 984: Line 984:
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
/*────────────────────────────────────────────────────────────────────────────*/
stuff: do k=1 for #; call lineout ,x.k; end; return</lang>
stuff: do k=1 for #; call lineout ,x.k; end; return</syntaxhighlight>
{{out|output|text=&nbsp; where showing the input and the output to the terminal:}}
{{out|output|text=&nbsp; where showing the input and the output to the terminal:}}
<pre>
<pre>
Line 997: Line 997:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
# Project : Input/Output for Lines of Text
# Project : Input/Output for Lines of Text


Line 1,014: Line 1,014:
see lines[i] + nl
see lines[i] + nl
next
next
</syntaxhighlight>
</lang>
Input:
Input:
<pre>
<pre>
Line 1,030: Line 1,030:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>def do_stuff(line)
<syntaxhighlight lang="ruby">def do_stuff(line)
puts line
puts line
end
end
Line 1,038: Line 1,038:
line = gets
line = gets
do_stuff(line)
do_stuff(line)
end</lang>
end</syntaxhighlight>


=={{header|Scala}}==
=={{header|Scala}}==
<lang Scala>// Input/Output for Lines of Text
<syntaxhighlight lang="scala">// Input/Output for Lines of Text
object IOLines extends App {
object IOLines extends App {
private val in = scala.io.StdIn
private val in = scala.io.StdIn
Line 1,052: Line 1,052:
doStuff(word)
doStuff(word)
}
}
}</lang>
}</syntaxhighlight>


=={{header|Tcl}}==
=={{header|Tcl}}==
<lang tcl>proc do_stuff {line} {
<syntaxhighlight lang="tcl">proc do_stuff {line} {
puts $line
puts $line
}
}
Line 1,061: Line 1,061:
foreach - [lrepeat [gets stdin] dummy] {
foreach - [lrepeat [gets stdin] dummy] {
do_stuff [gets stdin]
do_stuff [gets stdin]
}</lang>
}</syntaxhighlight>


=={{header|Ursa}}==
=={{header|Ursa}}==
<lang ursa>#
<syntaxhighlight lang="ursa">#
# input/output for lines of text
# input/output for lines of text
#
#
Line 1,084: Line 1,084:
out lines<i> endl console
out lines<i> endl console
end for
end for
</syntaxhighlight>
</lang>


=={{header|Wren}}==
=={{header|Wren}}==
This assumes that both Stdin and Stdout are connected to a terminal.
This assumes that both Stdin and Stdout are connected to a terminal.
<lang ecmascript>import "io" for Stdin
<syntaxhighlight lang="ecmascript">import "io" for Stdin


var output = Fn.new { |lines| System.print(lines.join("\n")) }
var output = Fn.new { |lines| System.print(lines.join("\n")) }
Line 1,097: Line 1,097:
for (i in 0...n) lines[i] = Stdin.readLine()
for (i in 0...n) lines[i] = Stdin.readLine()
System.print()
System.print()
output.call(lines)</lang>
output.call(lines)</syntaxhighlight>


{{out}}
{{out}}
Line 1,114: Line 1,114:
=={{header|XPL0}}==
=={{header|XPL0}}==
The input file must be redirected on the command line, for example: iotext <iotext.txt
The input file must be redirected on the command line, for example: iotext <iotext.txt
<lang XPL0>string 0;
<syntaxhighlight lang="xpl0">string 0;


proc PrintLn(Str); \"method" to print a line of text
proc PrintLn(Str); \"method" to print a line of text
Line 1,133: Line 1,133:
Line(I):= 0;
Line(I):= 0;
PrintLn(Line);
PrintLn(Line);
]</lang>
]</syntaxhighlight>


=={{header|zkl}}==
=={{header|zkl}}==
File ff.zkl:
File ff.zkl:
<lang zkl>numLines:=File.stdin.readln().strip().toInt();
<syntaxhighlight lang="zkl">numLines:=File.stdin.readln().strip().toInt();
text:=File.stdin.readln(numLines);
text:=File.stdin.readln(numLines);


text.apply(File.stdout.write);</lang>
text.apply(File.stdout.write);</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>