Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1 +1,7 @@
|
|||
{{selection|Short Circuit|Console Program Basics}}[[Category:Basic language learning]]Read from a text stream either word-by-word or line-by-line until the stream runs out of data. The stream will have an unknown amount of data on it.
|
||||
{{selection|Short Circuit|Console Program Basics}}[[Category:Basic language learning]] [[Category:Streams]] [[Category:Simple]]
|
||||
{{omit from|PARI/GP|No access to streams other than input}}
|
||||
{{omit from|TI-89 BASIC}} <!-- No streams other than user input, not really applicable. -->
|
||||
|
||||
Read from a text stream either word-by-word or line-by-line
|
||||
until the stream runs out of data. <br>
|
||||
The stream will have an unknown amount of data on it.
|
||||
|
|
|
|||
27
Task/Input-loop/Bracmat/input-loop.bracmat
Normal file
27
Task/Input-loop/Bracmat/input-loop.bracmat
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
( put$("This is
|
||||
a three line
|
||||
text","test.txt",NEW)
|
||||
& fil$("test.txt",r)
|
||||
& fil$(,STR," \t\r\n")
|
||||
& 0:?linenr
|
||||
& whl
|
||||
' ( fil$:(?line.?breakchar)
|
||||
& put
|
||||
$ ( str
|
||||
$ ( "breakchar:"
|
||||
( !breakchar:" "&SP
|
||||
| !breakchar:\t&"\\t"
|
||||
| !breakchar:\r&"\\r"
|
||||
| !breakchar:\n&"\\n"
|
||||
| !breakchar:&EOF
|
||||
)
|
||||
", word "
|
||||
(1+!linenr:?linenr)
|
||||
":"
|
||||
!line
|
||||
\n
|
||||
)
|
||||
)
|
||||
)
|
||||
& (fil$(,SET,-1)|out$"file closed")
|
||||
);
|
||||
|
|
@ -1,8 +1,23 @@
|
|||
import tango.io.Console;
|
||||
import tango.text.stream.LineIterator;
|
||||
void main() {
|
||||
import std.stdio;
|
||||
|
||||
void main (char[][] args) {
|
||||
foreach (line; new LineIterator!(char)(Cin.input)) {
|
||||
// do something with each line
|
||||
immutable fileName = "input_loop1.d";
|
||||
|
||||
foreach (const line; fileName.File.byLine) {
|
||||
pragma(msg, typeof(line)); // Prints: const(char[])
|
||||
// line is a transient slice, so if you need to
|
||||
// retain it for later use, you have to .dup or .idup it.
|
||||
line.writeln; // Do something with each line.
|
||||
}
|
||||
|
||||
// Keeping the line terminators:
|
||||
foreach (const line; fileName.File.byLine(KeepTerminator.yes)) {
|
||||
// line is a transient slice.
|
||||
line.writeln;
|
||||
}
|
||||
|
||||
foreach (const string line; fileName.File.lines) {
|
||||
// line is a transient slice.
|
||||
line.writeln;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
import tango.io.Console;
|
||||
import tango.text.stream.SimpleIterator;
|
||||
import tango.text.stream.LineIterator;
|
||||
|
||||
void main (char[][] args) {
|
||||
foreach (word; new SimpleIterator!(char)(" ", Cin.input)) {
|
||||
// do something with each word
|
||||
foreach (line; new LineIterator!(char)(Cin.input)) {
|
||||
// do something with each line
|
||||
}
|
||||
}
|
||||
|
|
|
|||
8
Task/Input-loop/D/input-loop-3.d
Normal file
8
Task/Input-loop/D/input-loop-3.d
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
import tango.io.Console;
|
||||
import tango.text.stream.SimpleIterator;
|
||||
|
||||
void main (char[][] args) {
|
||||
foreach (word; new SimpleIterator!(char)(" ", Cin.input)) {
|
||||
// do something with each word
|
||||
}
|
||||
}
|
||||
26
Task/Input-loop/Go/input-loop-1.go
Normal file
26
Task/Input-loop/Go/input-loop-1.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
in := bufio.NewReader(os.Stdin)
|
||||
for {
|
||||
s, err := in.ReadString('\n')
|
||||
if err != nil {
|
||||
// io.EOF is expected, anything else
|
||||
// should be handled/reported
|
||||
if err != io.EOF {
|
||||
log.Fatal(err)
|
||||
}
|
||||
break
|
||||
}
|
||||
// Do something with the line of text
|
||||
// in string variable s.
|
||||
_ = s
|
||||
}
|
||||
}
|
||||
24
Task/Input-loop/Go/input-loop-2.go
Normal file
24
Task/Input-loop/Go/input-loop-2.go
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := bufio.NewScanner(os.Stdin)
|
||||
// Select the split function, other ones are available
|
||||
// in bufio or you can provide your own.
|
||||
s.Split(bufio.ScanWords)
|
||||
for s.Scan() {
|
||||
// Get and use the next 'token'
|
||||
asBytes := s.Bytes() // Bytes does no alloaction
|
||||
asString := s.Text() // Text returns a newly allocated string
|
||||
_, _ = asBytes, asString
|
||||
}
|
||||
if err := s.Err(); err != nil {
|
||||
// Handle/report any error (EOF will not be reported)
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
in := bufio.NewReader(os.Stdin)
|
||||
for {
|
||||
s, err := in.ReadString('\n')
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
_ = s
|
||||
}
|
||||
}
|
||||
|
|
@ -8,7 +8,7 @@ lineNo = 0
|
|||
|
||||
loop label ioloop forever
|
||||
inLine = ask
|
||||
if inLine = null then leave ioloop -- stop on EOF
|
||||
if inLine = null then leave ioloop -- stop on EOF (Try Ctrl-D on UNIX-like systems or Ctrl-Z on Windows)
|
||||
lineNo = lineNo + 1
|
||||
lines[0] = lineNo
|
||||
lines[lineNo] = inLine
|
||||
|
|
|
|||
18
Task/Input-loop/Oberon-2/input-loop.oberon-2
Normal file
18
Task/Input-loop/Oberon-2/input-loop.oberon-2
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
MODULE InputLoop;
|
||||
IMPORT
|
||||
StdChannels,
|
||||
Channel;
|
||||
VAR
|
||||
reader: Channel.Reader;
|
||||
writer: Channel.Writer;
|
||||
c: CHAR;
|
||||
BEGIN
|
||||
reader := StdChannels.stdin.NewReader();
|
||||
writer := StdChannels.stdout.NewWriter();
|
||||
|
||||
reader.ReadByte(c);
|
||||
WHILE reader.res = Channel.done DO
|
||||
writer.WriteByte(c);
|
||||
reader.ReadByte(c)
|
||||
END
|
||||
END InputLoop.
|
||||
|
|
@ -4,6 +4,6 @@ open file (in) title ('/TEXT.DAT,type(text),recsize(200)' );
|
|||
on endfile (in) stop;
|
||||
|
||||
do forever;
|
||||
get edit (line) (L);
|
||||
get file(in) edit (line) (L);
|
||||
put skip list (line);
|
||||
end;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
/* -- AREXX -- */
|
||||
do until eof(stdin)
|
||||
l = readln(stdin)
|
||||
say l
|
||||
end
|
||||
Do Until input=''
|
||||
input=linein(stdin)
|
||||
Call lineout ,input
|
||||
End
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
/*REXX program to read from the (console) default input stream until nul*/
|
||||
|
||||
do until _==''
|
||||
parse pull _
|
||||
end /*until ...*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/* -- AREXX -- */
|
||||
do until eof(stdin)
|
||||
l = readln(stdin)
|
||||
say l
|
||||
end
|
||||
|
|
|
|||
6
Task/Input-loop/REXX/input-loop-4.rexx
Normal file
6
Task/Input-loop/REXX/input-loop-4.rexx
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
/*REXX program reads from the (console) default input stream until null*/
|
||||
|
||||
do until _==''
|
||||
parse pull _
|
||||
end /*until ...*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
6
Task/Input-loop/REXX/input-loop-5.rexx
Normal file
6
Task/Input-loop/REXX/input-loop-5.rexx
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
/*REXX program reads from the (console) default input stream until null*/
|
||||
|
||||
do until _==''
|
||||
_=linein()
|
||||
end /*until ...*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
Loading…
Add table
Add a link
Reference in a new issue