Input loop: Difference between revisions
Content added Content deleted
(add task to ARM64 assembly Raspberry Pi) |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 12: | Line 12: | ||
The stream will have an unknown amount of data on it. |
The stream will have an unknown amount of data on it. |
||
<br><br> |
<br><br> |
||
=={{header|AArch64 Assembly}}== |
=={{header|AArch64 Assembly}}== |
||
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
||
Line 176: | Line 177: | ||
.include "../includeARM64.inc" |
.include "../includeARM64.inc" |
||
</lang> |
</lang> |
||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
This example reads in a text stream from standard input line by line |
This example reads in a text stream from standard input line by line |
||
Line 284: | Line 286: | ||
ENDIF |
ENDIF |
||
ENDPROC</lang> |
ENDPROC</lang> |
||
=={{header|ARM Assembly}}== |
=={{header|ARM Assembly}}== |
||
{{works with|as|Raspberry Pi}} |
{{works with|as|Raspberry Pi}} |
||
Line 468: | Line 471: | ||
<lang awk>1</lang> |
<lang awk>1</lang> |
||
=={{header|Batch File}}== |
|||
<lang dos> |
|||
for /f %%i in (file.txt) do if %%i@ neq @ echo %%i |
|||
</lang> |
|||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
Line 536: | Line 534: | ||
190 CLOSE #1 |
190 CLOSE #1 |
||
200 END HANDLER</lang> |
200 END HANDLER</lang> |
||
=={{header|Batch File}}== |
|||
<lang dos> |
|||
for /f %%i in (file.txt) do if %%i@ neq @ echo %%i |
|||
</lang> |
|||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |
||
Line 609: | Line 612: | ||
} |
} |
||
return 0; |
return 0; |
||
}</lang> |
|||
=={{header|C sharp|C#}}== |
|||
<lang csharp>using System; |
|||
using System.IO; |
|||
class Program |
|||
{ |
|||
static void Main(string[] args) |
|||
{ |
|||
// For stdin, you could use |
|||
// new StreamReader(Console.OpenStandardInput(), Console.InputEncoding) |
|||
using (var b = new StreamReader("file.txt")) |
|||
{ |
|||
string line; |
|||
while ((line = b.ReadLine()) != null) |
|||
Console.WriteLine(line); |
|||
} |
|||
} |
|||
}</lang> |
}</lang> |
||
Line 691: | Line 715: | ||
} |
} |
||
</lang> |
</lang> |
||
=={{header|C sharp|C#}}== |
|||
<lang csharp>using System; |
|||
using System.IO; |
|||
class Program |
|||
{ |
|||
static void Main(string[] args) |
|||
{ |
|||
// For stdin, you could use |
|||
// new StreamReader(Console.OpenStandardInput(), Console.InputEncoding) |
|||
using (var b = new StreamReader("file.txt")) |
|||
{ |
|||
string line; |
|||
while ((line = b.ReadLine()) != null) |
|||
Console.WriteLine(line); |
|||
} |
|||
} |
|||
}</lang> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
Line 825: | Line 828: | ||
end; |
end; |
||
end.</lang> |
end.</lang> |
||
=={{header|Déjà Vu}}== |
=={{header|Déjà Vu}}== |
||
<lang dejavu>while /= :eof dup !read-line!stdin: |
<lang dejavu>while /= :eof dup !read-line!stdin: |
||
Line 1,080: | Line 1,084: | ||
end |
end |
||
</lang> |
</lang> |
||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
Line 1,154: | Line 1,157: | ||
Note: with GET(#1) you can read character by character. |
Note: with GET(#1) you can read character by character. |
||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
Line 1,269: | Line 1,271: | ||
<lang frink>while (line = readStdin[]) != undef |
<lang frink>while (line = readStdin[]) != undef |
||
println[line] |
println[line] |
||
</lang> |
|||
=={{header|FutureBasic}}== |
|||
Note: This code goes beyond simply specifying the file to open. It includes a dialog window that allows the user to select a text file to read. The entire contents of the file are read in at once, rather than line by line. |
|||
<lang futurebasic> |
|||
include "ConsoleWindow" |
|||
local fn ReadTextFile |
|||
dim as CFURLRef fileRef |
|||
dim as Handle h |
|||
dim as CFStringRef cfStr : cfStr = NULL |
|||
dim as long fileLen |
|||
if ( files$( _CFURLRefOpen, "TEXT", "Select text file...", @fileRef ) ) |
|||
open "i", 2, fileRef |
|||
fileLen = lof( 2, 1 ) |
|||
h = fn NewHandleClear( fileLen ) |
|||
if ( h ) |
|||
read file 2, [h], fileLen |
|||
close #2 |
|||
cfStr = fn CFStringCreateWithBytes( _kCFAllocatorDefault, #[h], fn GetHandleSize(h), _kCFStringEncodingMacRoman, _false ) |
|||
fn DisposeH( h ) |
|||
end if |
|||
else |
|||
// User canceled |
|||
end if |
|||
fn HIViewSetText( sConsoleHITextView, cfStr ) |
|||
CFRelease( cfStr ) |
|||
end fn |
|||
fn ReadTextFile |
|||
</lang> |
</lang> |
||
Line 1,281: | Line 1,315: | ||
It seems impossible to complete this task with just standard gnuplot commands. |
It seems impossible to complete this task with just standard gnuplot commands. |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
The following reads a line at a time from stdin. |
The following reads a line at a time from stdin. |
||
Line 1,338: | Line 1,373: | ||
} |
} |
||
}</lang> |
}</lang> |
||
=={{header|FutureBasic}}== |
|||
Note: This code goes beyond simply specifying the file to open. It includes a dialog window that allows the user to select a text file to read. The entire contents of the file are read in at once, rather than line by line. |
|||
<lang futurebasic> |
|||
include "ConsoleWindow" |
|||
local fn ReadTextFile |
|||
dim as CFURLRef fileRef |
|||
dim as Handle h |
|||
dim as CFStringRef cfStr : cfStr = NULL |
|||
dim as long fileLen |
|||
if ( files$( _CFURLRefOpen, "TEXT", "Select text file...", @fileRef ) ) |
|||
open "i", 2, fileRef |
|||
fileLen = lof( 2, 1 ) |
|||
h = fn NewHandleClear( fileLen ) |
|||
if ( h ) |
|||
read file 2, [h], fileLen |
|||
close #2 |
|||
cfStr = fn CFStringCreateWithBytes( _kCFAllocatorDefault, #[h], fn GetHandleSize(h), _kCFStringEncodingMacRoman, _false ) |
|||
fn DisposeH( h ) |
|||
end if |
|||
else |
|||
// User canceled |
|||
end if |
|||
fn HIViewSetText( sConsoleHITextView, cfStr ) |
|||
CFRelease( cfStr ) |
|||
end fn |
|||
fn ReadTextFile |
|||
</lang> |
|||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
Line 1,472: | Line 1,475: | ||
ghi |
ghi |
||
now is the time for all good men ...</lang> |
now is the time for all good men ...</lang> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
Some people prefer <tt>Scanner</tt> or <tt>BufferedReader</tt>, so a way with each is presented. |
Some people prefer <tt>Scanner</tt> or <tt>BufferedReader</tt>, so a way with each is presented. |
||
Line 1,888: | Line 1,892: | ||
lastCh := ch |
lastCh := ch |
||
END ReadName;</lang> |
END ReadName;</lang> |
||
=={{header|Modula-3}}== |
=={{header|Modula-3}}== |
||
Line 2,004: | Line 2,007: | ||
END InputLoop. |
END InputLoop. |
||
</pre> |
</pre> |
||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
<lang objeck> |
<lang objeck> |
||
Line 2,150: | Line 2,154: | ||
Hello 4 |
Hello 4 |
||
Hello 5</pre> |
Hello 5</pre> |
||
=={{header|Perl 6}}== |
|||
In Perl 6, filehandles etc. provide the <code>.lines</code> and <code>.words</code> methods which return lazy lists, and can thus they be iterated using a <code>for</code> loop... |
|||
'''Line-by-line''' <small>''(line endings are automatically stripped)''</small> |
|||
*From a file:<lang perl6>for "filename.txt".IO.lines -> $line { |
|||
... |
|||
}</lang> |
|||
*From standard input:<lang perl6>for $*IN.lines -> $line { |
|||
... |
|||
}</lang> |
|||
*From a pipe:<lang perl6>for run(«find -iname *.txt», :out).out.lines -> $filename { |
|||
... |
|||
}</lang> |
|||
*From a pipe, with custom line separator <small>''(in this example to handle filenames containing newlines)''</small>:<lang perl6>for run(«find -iname *.txt -print0», :nl«\0», :out).out.lines -> $filename { |
|||
... |
|||
}</lang> |
|||
'''Word-by-word''' |
|||
*From a file <lang perl6>for "filename.txt".IO.words -> $word { |
|||
... |
|||
}</lang> |
|||
*From standard input or a pipe, accordingly. |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 2,299: | Line 2,278: | ||
(copy-port (current-input-port) (current-output-port)) |
(copy-port (current-input-port) (current-output-port)) |
||
</lang> |
</lang> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
In Perl 6, filehandles etc. provide the <code>.lines</code> and <code>.words</code> methods which return lazy lists, and can thus they be iterated using a <code>for</code> loop... |
|||
'''Line-by-line''' <small>''(line endings are automatically stripped)''</small> |
|||
*From a file:<lang perl6>for "filename.txt".IO.lines -> $line { |
|||
... |
|||
}</lang> |
|||
*From standard input:<lang perl6>for $*IN.lines -> $line { |
|||
... |
|||
}</lang> |
|||
*From a pipe:<lang perl6>for run(«find -iname *.txt», :out).out.lines -> $filename { |
|||
... |
|||
}</lang> |
|||
*From a pipe, with custom line separator <small>''(in this example to handle filenames containing newlines)''</small>:<lang perl6>for run(«find -iname *.txt -print0», :nl«\0», :out).out.lines -> $filename { |
|||
... |
|||
}</lang> |
|||
'''Word-by-word''' |
|||
*From a file <lang perl6>for "filename.txt".IO.words -> $word { |
|||
... |
|||
}</lang> |
|||
*From standard input or a pipe, accordingly. |
|||
=={{header|REBOL}}== |
=={{header|REBOL}}== |
||
Line 2,390: | Line 2,395: | ||
''To open a new stream for reading, see [[Read a file line by line#Ruby]].'' |
''To open a new stream for reading, see [[Read a file line by line#Ruby]].'' |
||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
Line 2,399: | Line 2,403: | ||
wend |
wend |
||
close #f</lang> |
close #f</lang> |
||
=={{header|Scala}}== |
|||
{{libheader|Scala}} |
|||
{{works with|Scala|2.10.3}} |
|||
<lang scala> scala.io.Source.fromFile("input.txt").getLines().foreach { |
|||
line => ... }</lang> |
|||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
Line 2,427: | Line 2,425: | ||
}</lang> |
}</lang> |
||
=={{header| |
=={{header|Scala}}== |
||
{{libheader|Scala}} |
|||
<lang slate>(File newNamed: 'README') reader sessionDo: [| :input | input lines do: [| :line | inform: line]].</lang> |
|||
{{works with|Scala|2.10.3}} |
|||
<lang scala> scala.io.Source.fromFile("input.txt").getLines().foreach { |
|||
line => ... }</lang> |
|||
=={{header|sed}}== |
=={{header|sed}}== |
||
Line 2,473: | Line 2,474: | ||
} |
} |
||
}</lang> |
}</lang> |
||
=={{header|Slate}}== |
|||
<lang slate>(File newNamed: 'README') reader sessionDo: [| :input | input lines do: [| :line | inform: line]].</lang> |
|||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
Line 2,515: | Line 2,519: | ||
ENDACCESS source |
ENDACCESS source |
||
</lang> |
</lang> |
||
=={{header|UnixPipes}}== |
|||
the pipe 'yes XXX' produces a sequence |
|||
read by lines: |
|||
<lang bash>yes 'A B C D ' | while read x ; do echo -$x- ; done</lang> |
|||
read by words: |
|||
<lang bash>yes 'A B C D ' | while read -d\ a ; do echo -$a- ; done</lang> |
|||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
Line 2,534: | Line 2,530: | ||
Since <code>cat</code> defaults to reading from standard input and writing to standard output, this can be further simplified to the following. |
Since <code>cat</code> defaults to reading from standard input and writing to standard output, this can be further simplified to the following. |
||
<lang bash>cat</lang> |
<lang bash>cat</lang> |
||
=={{header|UnixPipes}}== |
|||
the pipe 'yes XXX' produces a sequence |
|||
read by lines: |
|||
<lang bash>yes 'A B C D ' | while read x ; do echo -$x- ; done</lang> |
|||
read by words: |
|||
<lang bash>yes 'A B C D ' | while read -d\ a ; do echo -$a- ; done</lang> |
|||
=={{header|Ursa}}== |
=={{header|Ursa}}== |
||
Line 2,562: | Line 2,566: | ||
stream.Close |
stream.Close |
||
End Sub</lang> |
End Sub</lang> |
||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
<lang vb> |
<lang vb> |