Difference between revisions of "File to array"
Jump to navigation
Jump to search
(Created page with "<onlyinclude>{{{{{format|Function2}}} |name=file_to_array |function1.return_type={{type|string}} <nowiki>[</nowiki>{{type|int}}<nowiki>]</nowiki> |function1.description=Reads...") |
|||
Line 11: | Line 11: | ||
|code1={{CodeSample | |code1={{CodeSample | ||
|title=Code Samples | |title=Code Samples | ||
− | |description=Reads a data file into a map of strings | + | |description=Reads a data file into a map of strings and prints each line |
|code= | |code= | ||
− | <syntaxhighlight lang=" | + | <syntaxhighlight lang="d"> |
string [int] lines = file_to_array("mydata.txt"); | string [int] lines = file_to_array("mydata.txt"); | ||
+ | foreach line_no, line in lines { | ||
+ | print(`{line_no}: {line}`); | ||
+ | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
|moreinfo= | |moreinfo= |
Revision as of 05:05, 21 December 2020
Function Syntax
string [int] file_to_array( string filename )
- Reads data from a text file and returns a map of strings containing each line.
- filename: Path to the text file
The returned value is actually a map of strings, one string for each line. Like file_to_map()
, empty lines and those starting with a #
are ignored.
Code Samples
Reads a data file into a map of strings and prints each line
string [int] lines = file_to_array("mydata.txt");
foreach line_no, line in lines {
print(`{line_no}: {line}`);
}