Read a specific line from a file: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 397:
}
</lang>
 
=={{header|C sharp}}==
 
<lang C sharp>using System;
using System.IO;
 
namespace GetLine
{
internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine(GetLine(args[0], uint.Parse(args[1])));
}
 
private static string GetLine(string path, uint line)
{
using (var reader = new StreamReader(path))
{
try
{
for (uint i = 0; i <= line; i++)
{
if (reader.EndOfStream)
return string.Format("There {1} less than {0} line{2} in the file.", line,
((line == 1) ? "is" : "are"), ((line == 1) ? "" : "s"));
 
if (i == line)
return reader.ReadLine();
 
reader.ReadLine();
}
}
catch (IOException ex)
{
return ex.Message;
}
catch (OutOfMemoryException ex)
{
return ex.Message;
}
}
 
throw new Exception("Something bad happened.");
}
}
}</lang>
 
=={{header|C++}}==
Line 463 ⟶ 510:
CL-USER> (read-nth-line "/tmp/test1.text" 7)
"foo"
 
=={{header|C sharp}}==
 
<lang C sharp>using System;
using System.IO;
 
namespace GetLine
{
internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine(GetLine(args[0], uint.Parse(args[1])));
}
 
private static string GetLine(string path, uint line)
{
using (var reader = new StreamReader(path))
{
try
{
for (uint i = 0; i <= line; i++)
{
if (reader.EndOfStream)
return string.Format("There {1} less than {0} line{2} in the file.", line,
((line == 1) ? "is" : "are"), ((line == 1) ? "" : "s"));
 
if (i == line)
return reader.ReadLine();
 
reader.ReadLine();
}
}
catch (IOException ex)
{
return ex.Message;
}
catch (OutOfMemoryException ex)
{
return ex.Message;
}
}
 
throw new Exception("Something bad happened.");
}
}
}</lang>
 
=={{header|D}}==
Line 563:
end
</lang>
 
=={{header|Erlang}}==
Using function into_list/1 from [[Read_a_file_line_by_line]].
Line 612 ⟶ 613:
in function read_a_specific_line:line_nr/2 (read_a_specific_line.erl, line 25)
</pre>
 
=={{header|F_Sharp|F#}}==
<lang fsharp>open System
open System.IO
 
[<EntryPoint>]
let main args =
let n = Int32.Parse(args.[1]) - 1
use r = new StreamReader(args.[0])
let lines = Seq.unfold (
fun (reader : StreamReader) ->
if (reader.EndOfStream) then None
else Some(reader.ReadLine(), reader)) r
let line = Seq.nth n lines // Seq.nth throws an ArgumentException,
// if not not enough lines available
Console.WriteLine(line)
0</lang>
 
=={{header|Factor}}==
Line 757 ⟶ 775:
Print "Press any key to quit"
Sleep </lang>
 
=={{header|F_Sharp|F#}}==
<lang fsharp>open System
open System.IO
 
[<EntryPoint>]
let main args =
let n = Int32.Parse(args.[1]) - 1
use r = new StreamReader(args.[0])
let lines = Seq.unfold (
fun (reader : StreamReader) ->
if (reader.EndOfStream) then None
else Some(reader.ReadLine(), reader)) r
let line = Seq.nth n lines // Seq.nth throws an ArgumentException,
// if not not enough lines available
Console.WriteLine(line)
0</lang>
 
=={{header|FutureBasic}}==
Line 1,051 ⟶ 1,052:
#this_line // 6th, which is the 7th line in the file
</lang>
 
=={{header|Lua}}==
<lang lua>function fileLine (lineNum, fileName)
local count = 0
for line in io.lines(fileName) do
count = count + 1
if count == lineNum then return line end
end
error(fileName .. " has fewer than " .. lineNum .. " lines.")
end
 
print(fileLine(7, "test.txt"))</lang>
 
=={{header|Liberty BASIC}}==
Line 1,078 ⟶ 1,067:
print line7$</lang>
 
=={{header|Lua}}==
<lang lua>function fileLine (lineNum, fileName)
local count = 0
for line in io.lines(fileName) do
count = count + 1
if count == lineNum then return line end
end
error(fileName .. " has fewer than " .. lineNum .. " lines.")
end
 
print(fileLine(7, "test.txt"))</lang>
 
=={{header|Maple}}==
Line 1,090:
elif i <= num then printf ("Line number %d is not reached",num): end if:
end proc:</lang>
 
 
=={{header|Mathematica}}==
Line 1,288 ⟶ 1,287:
while (<>) { $. == $n and print, exit }
die "file too short\n";</lang>
 
=={{header|Perl 6}}==
<lang perl6>say lines[6] // die "Short file";</lang>
Without an argument, the <tt>lines</tt> function reads filenames from the command line, or defaults to standard input. It then returns a lazy list, which we subscript to get the 7th element. Assuming this code is in a program called <tt>line7</tt>:
<pre>$ cal 2011 > cal.txt
$ line7 cal.txt
16 17 18 19 20 21 22 20 21 22 23 24 25 26 20 21 22 23 24 25 26
$</pre>
 
This works even on infinite files because lists are lazy:
<pre>$ yes | line7
y
$</pre>
 
=={{header|Phix}}==
Line 1,370 ⟶ 1,356:
$file | Where Readcount -eq 7 | set-variable -name Line7
}
</lang>
 
=={{header|Python}}==
 
Using only builtins (note that <code>enumerate</code> is zero-based):
 
<lang python>with open('xxx.txt') as f:
for i, line in enumerate(f):
if i == 6:
break
else:
print('Not 7 lines in file')
line = None</lang>
 
Using the <code>islice</code> iterator function from the [https://docs.python.org/3/library/itertools.html#itertools.islice itertools] standard library module, which applies slicing to an iterator and thereby skips over the first six lines:
 
<lang python>from itertools import islice
 
with open('xxx.txt') as f:
try:
line = next(islice(f, 6, 7))
except StopIteration:
print('Not 7 lines in file')</lang>
 
Similar to the Ruby implementation, this will read up to the first 7 lines, returning only the last. Note that the 'readlines' method reads the entire file contents into memory first as opposed to using the file iterator itself which is more performant for large files.
<lang python>
print open('xxx.txt').readlines()[:7][-1]
</lang>
 
Line 1,432 ⟶ 1,391:
EndIf
EndIf </lang>
 
=={{header|Python}}==
 
Using only builtins (note that <code>enumerate</code> is zero-based):
 
<lang python>with open('xxx.txt') as f:
for i, line in enumerate(f):
if i == 6:
break
else:
print('Not 7 lines in file')
line = None</lang>
 
Using the <code>islice</code> iterator function from the [https://docs.python.org/3/library/itertools.html#itertools.islice itertools] standard library module, which applies slicing to an iterator and thereby skips over the first six lines:
 
<lang python>from itertools import islice
 
with open('xxx.txt') as f:
try:
line = next(islice(f, 6, 7))
except StopIteration:
print('Not 7 lines in file')</lang>
 
Similar to the Ruby implementation, this will read up to the first 7 lines, returning only the last. Note that the 'readlines' method reads the entire file contents into memory first as opposed to using the file iterator itself which is more performant for large files.
<lang python>
print open('xxx.txt').readlines()[:7][-1]
</lang>
 
=={{header|R}}==
Line 1,452 ⟶ 1,438:
(λ(i) (for/last ([line (in-lines i)] [n 7]) line))))
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<lang perl6>say lines[6] // die "Short file";</lang>
Without an argument, the <tt>lines</tt> function reads filenames from the command line, or defaults to standard input. It then returns a lazy list, which we subscript to get the 7th element. Assuming this code is in a program called <tt>line7</tt>:
<pre>$ cal 2011 > cal.txt
$ line7 cal.txt
16 17 18 19 20 21 22 20 21 22 23 24 25 26 20 21 22 23 24 25 26
$</pre>
 
This works even on infinite files because lists are lazy:
<pre>$ yes | line7
y
$</pre>
 
=={{header|REBOL}}==
Line 1,642:
</lang>
That is we remember (h) the line, if any, in hold space. At last line ($) we exchange (x) pattern space and hold space. If hold space was empty -- print error message.
 
=={{header|Seed7}}==
The function ''getLine'' skips lines with [http://seed7.sourceforge.net/libraries/file.htm#readln%28inout_file%29 readln]
10,333

edits