File to array: Difference between revisions

From Kolmafia
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..."
 
Fix link
 
(4 intermediate revisions by one other user not shown)
Line 8: Line 8:
|function1.param2.default=
|function1.param2.default=
|function1.param1.description=Path to the text file
|function1.param1.description=Path to the text file
|description=The returned value is actually a map of strings, one string for each line. Like <code>[[file_to_map|file_to_map()]]</code>, empty lines and those starting with a <code>#</code> are ignored.
|description=<p>The returned value is actually a map of strings, one string for each line. Like <code>[[file_to_map|file_to_map()]]</code>, empty lines and those starting with a <code>#</code> are ignored.</p>
 
<p>This function was added in [https://kolmafia.us/threads/file_to_string-or-file_to_map-string-string-or-session_logs-string-int-int.22940/#post-148463 r18740].</p>
|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="java">
<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=
}}
}}
|see_also={{SeeAlso|file_to_map|map_to_file}}
|see_also={{SeeAlso|buffer_to_file|file_to_buffer|file_to_map|map_to_file}}
|{{{1|}}}
|{{{1|}}}
}}</onlyinclude>
}}</onlyinclude>
[[Category:Miscellaneous Functions]]
[[Category:Miscellaneous Functions]]

Latest revision as of 06:38, 24 April 2023

Function Syntax

string [int] file_to_arraystring 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.

This function was added in r18740.

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}`);
}

See Also

buffer_to_file() | file_to_buffer() | file_to_map() | map_to_file()