Append a record to the end of a text file: Difference between revisions

m
syntax highlighting fixup automation
(Added a Scheme implementation.)
m (syntax highlighting fixup automation)
Line 62:
 
=={{header|Action!}}==
<langsyntaxhighlight lang=Action!>PROC CreateLog(CHAR ARRAY fname)
CHAR ARRAY header="account:password:UID:GID:fullname,office,extension,homephone,email:directory:shell"
BYTE dev=[1]
Line 129:
 
LMARGIN=oldLMARGIN ;restore left margin on the screen
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Append_a_record_to_the_end_of_a_text_file.png Screenshot from Atari 8-bit computer]
Line 146:
 
=={{header|Ada}}==
<langsyntaxhighlight lang=Ada>
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
Line 267:
Close(The_File);
end Main;
</syntaxhighlight>
</lang>
{{output}}
<pre>
Line 279:
 
=={{header|AWK}}==
<langsyntaxhighlight lang=AWK>
# syntax: GAWK -f APPEND_A_RECORD_TO_THE_END_OF_A_TEXT_FILE.AWK
BEGIN {
Line 304:
printf("%d records\n\n",nr)
}
</syntaxhighlight>
</lang>
<p>Output:</p>
<pre>
Line 322:
 
=={{header|Batch File}}==
<langsyntaxhighlight lang=dos>
@echo off
 
Line 339:
type append.txt
pause>nul
</syntaxhighlight>
</lang>
 
{{out}}
Line 370:
 
'''From a C "struct" to CSV File'''
<langsyntaxhighlight lang=C>#include <stdio.h>
#include <string.h>
/* note that UID & GID are of type "int" */
Line 422:
if(strstr(passwd_buf, "xyz"))
printf("Appended record: %s\n", passwd_buf);
}</langsyntaxhighlight>
{{out}}
<pre>
Line 429:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang=csharp>using System;
using System.IO;
 
Line 472:
}
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 480:
=={{header|C++}}==
{{trans|C#}}
<langsyntaxhighlight lang=cpp>#include <iostream>
#include <fstream>
#include <string>
Line 560:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Appended record: xyz:x:1003:1000:X Yz,Room 1003,(234)555-8913,(234)555-0033,xyz@rosettacode.org:/home/xyz:/bin/bash</pre>
Line 611:
 
'''Record group to passwd format, LINE SEQUENTIAL'''
<langsyntaxhighlight lang=COBOL>
*> Tectonics:
*> cobc -xj append.cob
Line 883:
.
 
end program append.</langsyntaxhighlight>
{{out}}
<pre>prompt$ cobc -xj append.cob
Line 891:
=={{header|Common Lisp}}==
Tested on CLISP 2.49
<langsyntaxhighlight lang=lisp>(defvar *initial_data*
(list
(list "jsmith" "x" 1001 1000
Line 943:
 
(main)
</syntaxhighlight>
</lang>
{{out}}
<pre>$ clisp append_file.cl
Line 964:
=={{header|D}}==
{{trans|Java}}
<langsyntaxhighlight lang=D>class Record {
private const string account;
private const string password;
Line 1,056:
}
passwd.close();
}</langsyntaxhighlight>
 
{{out}}
Line 1,077:
| objects (subclass of Struct builtin) || text file || builtin || ☑ || ☑ || ☒
|}
<langsyntaxhighlight lang=elixir>
defmodule Gecos do
defstruct [:fullname, :office, :extension, :homephone, :email]
Line 1,164:
 
Appender.write("passwd.txt")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,177:
In the absence of a length indication, when outputting text variables with a view to having them read in again, delimiting them with quotes (and doubling internal quotes) is the route to peace of mind, because otherwise a text might contain a comma as in "43 Gurney Road, Belmont" and in the absence of quoting, such a sequence on input might well be taken as two fields rather than one and a mess is certain. This facility is offered by the free-format (or, "list directed") style initiated by the use of * in place of a format label, as in <code>WRITE (MSG,*) ''etc.''</code>, provided however that the output file has been opened with the optional usage attribute <code>DELIM = "QUOTE"</code>, an unfortunate choice of name, because the field separator character could also be termed a "delimiter" and the default value is both a space or comma, when often what is preferable is only a comma, or even a tab. But there is no standard method to specify this desire. Only if the texts never themselves contain commas or spaces will this unquoted free-format scheme evade extra effort, and the specified example precludes this simplicity.
 
The resulting output is strung along an output line, with a default line length of 132 (a standard lineprinter width); the specification of RECL = 666 ensures that all the output fields are rolled to one line - that isn't padded out to 666 characters: this is not a fixed-length record despite the specification of RECL as a constant, though each record turns out to be 248 characters long. Unfortunately, the trailing spaces in each character variable are rolled forth and there is no option along the lines of <code>WRITE (MSG,*) TRIM(NOTE)</code> One could instead use a suitable FORMAT statement with "A" format codes, but every element of NOTE would have to be named in the output list and the TRIM function applied only to each character field. Not only would this be tedious and error-prone, there is no format code for the enquoting and double-quoting of the texts and thus, no peace of mind... One would of course devise a subroutine to write out such a record (which would probably be more complex, with the FULLNAME subdivided, etc.), but the task's main objective is to demonstrate appending output to a file.<langsyntaxhighlight lang=Fortran> PROGRAM DEMO !As per the described task, more or less.
TYPE DETAILS !Define a component.
CHARACTER*28 FULLNAME
Line 1,240:
Closedown.
20 CLOSE (F)
END</langsyntaxhighlight>
 
The output can be read back in with the same free-format style. The fields are separated by spaces (outside a quoted string) though commas are also allowed. The file content is as follows:
Line 1,302:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang=freebasic>' FB 1.05.0 Win64
 
Type Person
Line 1,387:
Open "passwd.txt" For Append Lock Write As #1
Print #1, records(3)
Close #1</langsyntaxhighlight>
 
{{out}}
Line 1,415:
''See also [[#Pascal|Pascal]]''
{{works with|Linux}}
<langsyntaxhighlight lang=delphi>{$mode objFPC}
{$longStrings on}
{$modeSwitch classicProcVars+}
Line 1,572:
writeLn(line);
end;
end.</langsyntaxhighlight>
{{out}}
<pre>jsmith:x:1001:1000:Joe Smith,Room 1007,(234)555-8917,(234)555-0077,jsmith@rosettacode.org:/home/jsmith:/bin/bash
Line 1,593:
 
=={{header|Go}}==
<langsyntaxhighlight lang=go>package main
 
import (
Line 1,693:
fmt.Println("it didn't work")
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,721:
=={{header|Groovy}}==
Solution:
<langsyntaxhighlight lang=groovy>class PasswdRecord {
String account, password, directory, shell
int uid, gid
Line 1,788:
w.append('\r\n')
}
}</langsyntaxhighlight>
 
Test:
<langsyntaxhighlight lang=groovy>def checkPasswdFile = { it ->
File passwd = new File('passwd.txt')
assert passwd.exists()
Line 1,811:
 
println "File contents after new record added"
checkPasswdFile()</langsyntaxhighlight>
 
Output:
Line 1,825:
=={{header|Haskell}}==
Solution:
<langsyntaxhighlight lang=haskell>
{-# LANGUAGE RecordWildCards #-}
 
Line 1,855:
addRecord :: String -> Record -> IO ()
addRecord path r = appendFile path (show r)
</syntaxhighlight>
</lang>
 
Test:
<langsyntaxhighlight lang=haskell>
t1 = Record "jsmith" "x" 1001 1000 "/home/jsmith" "/bin/bash"
(Gecos "Joe Smith" "Room 1007" "(234)555-8917" "(234)555-0077" "jsmith@rosettacode.org")
Line 1,873:
lastLine <- fmap (last . lines) (readFile path)
putStrLn lastLine
</syntaxhighlight>
</lang>
 
=={{header|Icon}} and {{header|Unicon}}==
Works in both languages:
<langsyntaxhighlight lang=unicon>procedure main()
orig := [
"jsmith:x:1001:1000:Joe Smith,Room 1007,(234)555-8917,(234)555-0077,jsmith@rosettacode.org:/home/jsmith:/bin/bash",
Line 1,889:
every (f := open(fName,"a")) | write(f,!new) | close(f)
every (f := open(fName,"r")) | write(!f) | close(f)
end</langsyntaxhighlight>
 
Run:
Line 1,914:
 
=={{header|J}}==
<langsyntaxhighlight lang=j>require'strings ~system/packages/misc/xenos.ijs'
record=: [:|: <@deb;._1@(':',]);._2@do bind '0 :0'
 
Line 1,956:
R3 fappend passwd
 
assert 1 e. R3 E. fread passwd </langsyntaxhighlight>
 
{|class="wikitable" style="text-align: center; margin: 1em auto 1em auto;"
Line 1,972:
=={{header|Java}}==
 
<langsyntaxhighlight lang=java>import static java.util.Objects.requireNonNull;
 
import java.io.IOException;
Line 2,042:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Appended Record: xyz:x:1003:1000:X Yz,Room 1003,(234)555-8913,(234)555-0033,xyz@rosettacode.org:/home/xyz:/bin/bash</pre>
 
=={{header|Julia}}==
<langsyntaxhighlight lang=julia>
using SHA # security instincts say do not write bare passwords to a shared file even in toy code :)
 
Line 2,094:
writepasswd(pfile, xyz)
println("After last record added, file is:\n$(readstring(pfile))")
</syntaxhighlight>
</lang>
{{output}}<pre>
Before last record added, file is:
Line 2,108:
=={{header|Kotlin}}==
{{works with|Ubuntu 16.04}}
<langsyntaxhighlight lang=scala>// Version 1.2.41
 
import java.io.File
Line 2,163:
println(parseRecord(it))
}
}</langsyntaxhighlight>
 
{{output}}
Line 2,194:
 
=={{header|Lua}}==
<langsyntaxhighlight lang=lua>function append(tbl,filename)
local file,err = io.open(filename, "a")
if err then return err end
Line 2,245:
xyz.directory = "/home/xyz"
xyz.shell = "/bin/bash"
append(xyz, ".passwd")</langsyntaxhighlight>
=={{header|M2000 Interpreter}}==
<langsyntaxhighlight lang=M2000 Interpreter>Module TestThis {
Class passwd {
account$, password$
Line 2,327:
}
TestThis
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,336:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight lang=Mathematica>data = <|"account" -> "xyz", "password" -> "x", "UID" -> 1003,
"GID" -> 1000, "fullname" -> "X Yz", "office" -> "Room 1003",
"extension" -> "(234)555-8913", "homephone" -> "(234)555-0033",
Line 2,352:
str = OpenWrite[fname]; (* Use OpenAppend if file exists *)
Close[str];
Print["Appended record: " <> asString[data]];</langsyntaxhighlight>
{{out}}
<pre>Appended record: xyz:x:1003:1000:X Yz,Room 1003,(234)555-8913,(234)555-0033,xyz@rosettacode.org:/home/xyz:/bin/bash</pre>
Line 2,370:
 
=={{header|MATLAB}} / {{header|Octave}}==
<langsyntaxhighlight lang=Matlab> DS{1}.account='jsmith';
DS{1}.password='x';
DS{1}.UID=1001;
Line 2,427:
printf('%s\n',fgetl(fid));
end;
fclose(fid); </langsyntaxhighlight>
{{out}}
<pre>jsmith:x:1001:1000:Joe Smith,Room 1007,(234)555-8917,(234)555-0077,jsmith@rosettacode.org:/home/jsmith/bin/bash
Line 2,442:
 
As several other solutions, we have chosen to parse the "passwd" lines even if it not required.
<langsyntaxhighlight lang=Nim>import posix
import strutils
 
Line 2,589:
echo "-------------------------------------"
for line in lines(FileName):
echo line</langsyntaxhighlight>
 
{{out}}
Line 2,648:
''See also: [[#Free Pascal|Free Pascal]]''
{{works with|Extended Pascal}}
<langsyntaxhighlight lang=pascal>program appendARecordToTheEndOfATextFile;
 
var
Line 2,694:
{ write another record to file }
writeLn(passwd, 'xyz:x:1003:1000:X Yz,Room 1003,(234)555-8913,(234)555-0033,xyz@rosettacode.org:/home/xyz:/bin/bash');
end.</langsyntaxhighlight>
 
{| class="wikitable" style="text-align: center; margin: 1em auto;"
Line 2,712:
=={{header|Perl}}==
The program uses flock(2) (or emulation, if flock is not available) to lock the file.
<langsyntaxhighlight lang=Perl>use strict;
use warnings;
 
Line 2,818:
);
}
</syntaxhighlight>
</lang>
 
Output:
Line 2,865:
same process, in which case locking does not make any real difference - because there is no task_yield() in a locked state.
You can also test the locking when running multiple processes/threads by uncommenting the wait_key() lines.
<!--<langsyntaxhighlight lang=Phix>(notonline)-->
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (file i/o, sleep, task_yield, wait_key)</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">filename</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"passwd.txt"</span>
Line 2,928:
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #7060A8;">close</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,941:
 
=={{header|Phixmonti}}==
<langsyntaxhighlight lang=Phixmonti>include ..\Utilitys.pmt
 
def ltos /# l -- s #/
Line 2,996:
endif
endwhile
f fclose</langsyntaxhighlight>
{{out}}
<pre>["jsmith", "x", "1001", "1000", ["Joe Smith", "Room 1007", "(234)555-8917", "(234)555-0077", "jsmith@rosettacode.org"], "/home/jsmith", "/bin/bash"]
Line 3,020:
|}
 
<langsyntaxhighlight lang=PHP><?php
 
$filename = '/tmp/passwd';
Line 3,039:
 
echo 'File contents after new record added:', PHP_EOL, file_get_contents($filename), PHP_EOL;
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,054:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight lang=PicoLisp>(setq L '((jsmith x 1001 1000
"Joe Smith,Room 1007,(234)555-8917,\
(234)555-0077,jsmith@rosettacode.org"
Line 3,080:
(= 3 (lines "mythical") ) ) ) )
 
(bye)</langsyntaxhighlight>
 
=={{header|PowerShell}}==
Line 3,086:
 
Since PowerShell loves to deal in objects I wrote extra code to better manipilate and display data.
<langsyntaxhighlight lang=PowerShell>
function Test-FileLock
{
Line 3,250:
}
}
</syntaxhighlight>
</lang>
Create record objects.
<langsyntaxhighlight lang=PowerShell>
$records = @()
 
Line 3,278:
-Directory '/home/jdoe' `
-Shell '/bin/bash'
</syntaxhighlight>
</lang>
Display record objects.
<langsyntaxhighlight lang=PowerShell>
$records | Format-Table -AutoSize
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 3,291:
</pre>
Export records to file.
<langsyntaxhighlight lang=PowerShell>
if (-not(Test-FileLock -Path ".\passwd.txt"))
{
$records | Export-File -Path ".\passwd.txt"
}
</syntaxhighlight>
</lang>
This is the file.
<langsyntaxhighlight lang=PowerShell>
Get-Content -Path ".\passwd.txt"
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 3,307:
</pre>
Add a record to the record set.
<langsyntaxhighlight lang=PowerShell>
$records+= New-Record -Account 'xyz' `
-Password 'x' `
Line 3,319:
-Directory '/home/xyz' `
-Shell '/bin/bash'
</syntaxhighlight>
</lang>
Display record objects, sorted on last name; and display them.
<langsyntaxhighlight lang=PowerShell>
$records | Sort-Object { $_.GECOS.FullName.Split(" ")[1] } | Format-Table -AutoSize
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 3,333:
</pre>
Export records to file.
<langsyntaxhighlight lang=PowerShell>
if (-not(Test-FileLock -Path ".\passwd.txt"))
{
$records | Export-File -Path ".\passwd.txt"
}
</syntaxhighlight>
</lang>
This is the file.
<langsyntaxhighlight lang=PowerShell>
Get-Content -Path ".\passwd.txt"
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 3,393:
|}
'''From a "dict" to a CSV File'''
<langsyntaxhighlight lang=python>#############################
# Create a passwd text file
#############################
Line 3,440:
passwd_list=list(open("passwd.txt","r"))
if "xyz" in passwd_list[-1]:
print "Appended record:",passwd_list[-1][:-1]</langsyntaxhighlight>
{{out}}
<pre>Appended record: xyz:x:1003:1000:X Yz,Room 1003,(234)555-8913,(234)555-0033,xyz@rosettacode.org:/home/xyz:/bin/bash</pre>
Line 3,462:
|}
 
<langsyntaxhighlight lang=racket>
#lang racket
 
Line 3,499:
 
(printf "Looking up xyz in current file:\n=> ~s\n" (lookup "xyz"))
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,515:
This is kind of silly as it takes a string, converts it to a record, and then instantly converts it back to a string to write out to a file. Most of the "record handling" code is just demonstrating a possible way to store records in memory. It really has nothing to do with appending a string to a file.
 
<langsyntaxhighlight lang=perl6>class record {
has $.name;
has $.password;
Line 3,581:
put "Last line of $fname after append:";
put $fname.IO.lines.tail;
</syntaxhighlight>
</lang>
{{out}}
<pre>Last line of foo.fil before append:
Line 3,619:
|}
'''The easy short solution'''
<syntaxhighlight lang=vb>
<lang vb>
'Short solution: Append record and read last record
$Include "Rapidq.inc"
Line 3,648:
 
showmessage "Appended record: " + LogRec
</syntaxhighlight>
</lang>
{{out}}
<pre>Appended record: xyz:x:1003:1000:X Yz,Room 1003,(234)555-8913,(234)555-0033,xyz@rosettacode.org:/home/xyz:/bin/bash</pre>
 
'''Full solution: create an object to handle all functions'''
<syntaxhighlight lang=vb>
<lang vb>
'Full solution: Create an object with all required fields and
'build-in functions to append a record and to read the last record
Line 3,799:
Print ""
input "Press enter to exit:";a$
</syntaxhighlight>
</lang>
 
=={{header|REXX}}==
Line 3,817:
The data fields for the three records were coded on two statements instead of
<br>continuing them on separate statements for brevity.
<langsyntaxhighlight lang=rexx>/*REXX program writes (appends) two records, closes the file, appends another record.*/
tFID= 'PASSWD.TXT' /*define the name of the output file.*/
call lineout tFID /*close the output file, just in case,*/
Line 3,852:
 
say '***error***'; say r 'record's(r) "not written to file" fid; exit 13
/*some error causes: no write access, disk is full, file lockout, no authority*/</langsyntaxhighlight>
'''PASSWD.TXT''' file before the REXX program ran:
<pre>
Line 3,879:
| objects (subclass of Struct builtin) || text file || builtin || ☑ || ☑ || ☒
|}
<langsyntaxhighlight lang=ruby>Gecos = Struct.new :fullname, :office, :extension, :homephone, :email
class Gecos
def to_s
Line 3,914:
 
puts "after appending:"
puts File.readlines(filename)</langsyntaxhighlight>
{{out}}
<pre>
Line 3,927:
 
=={{header|Rust}}==
<langsyntaxhighlight lang=rust>
use std::fs::File;
use std::fs::OpenOptions;
Line 4,099:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 4,113:
 
=={{header|Scala}}==
{{libheader|Scala}}<langsyntaxhighlight lang=scala>import java.io.{File, FileWriter, IOException}
import scala.io.Source
 
Line 4,168:
case e: IOException => println(s"Running Example failed: ${e.getMessage}")
}
} // 57 lines</langsyntaxhighlight>
=={{header|Scheme}}==
{{works with|Chez Scheme}}
<langsyntaxhighlight lang=scheme>; Join list of strings into single string, with given separator between elements.
(define string-join
(lambda (str-lst sep)
Line 4,226:
(make-transcoder (latin-1-codec) 'lf))))
(format op "~a~%" record3)
(close-output-port op)))</langsyntaxhighlight>
{{out}}
<pre>
Line 4,263:
|}
 
<langsyntaxhighlight lang=ruby>define (
RECORD_FIELDS = %w(account password UID GID GECOS directory shell),
GECOS_FIELDS = %w(fullname office extension homephone email),
Line 4,360:
'(234)555-0033,xyz@rosettacode.org:/home/xyz:/bin/bash')
 
say "** Test passed!"</langsyntaxhighlight>
 
Note that flock uses advisory lock; some other program (if it doesn't use flock) can still unexpectedly write to the file.
Line 4,379:
|}
Note that appending is only safe on POSIX OSes where the data is written in “small enough” amounts to a local disk. This is a limitation of the OS APIs.
<langsyntaxhighlight lang=tcl># Model the data as nested lists, as that is a natural fit for Tcl
set basicRecords {
{
Line 4,463:
set recs [readRecords $f]
close $f
puts "last record is for [lindex $recs end 0], named [lindex $recs end 4 0]"</langsyntaxhighlight>{{out}}
last record is for xyz, named X Yz
The file will have this content:
Line 4,484:
| one-dimensional arrays (indexed or associative) || text file || builtin (shell redirections) || ☑ || ☑ || OS defined
|}
<langsyntaxhighlight lang=bash>rec1=(
jsmith
x
Line 4,532:
 
echo after appending:
cat "$filename"</langsyntaxhighlight>
 
{{output}}
Line 4,545:
=={{header|Ursa}}==
{{trans|Awk}}
<langsyntaxhighlight lang=ursa># ursa appends to files by default when the out function is used
 
# create new passwd in working directory
Line 4,572:
while (f.hasline)
out (in string f) endl console
end while</langsyntaxhighlight>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang=vbnet>Imports System.IO
 
Module Module1
Line 4,629:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>Appended record: xyz:x:1003:1000:X Yz,Room 1003,(234)555-8913,(234)555-0033,xyz@rosettacode.org:/home/xyz:/bin/bash</pre>
Line 4,637:
 
To append a record to an existing file in a CLI script, a little digging is required as the method to do this (''File.openWithFlags'') is currently undocumented. However, the following works fine.
<langsyntaxhighlight lang=ecmascript>import "io" for File, FileFlags
 
var records = [
Line 4,665:
contents = File.read(fileName)
System.print("Records after appending new one:\n")
System.print(contents)</langsyntaxhighlight>
 
{{out}}
Line 4,697:
 
=={{header|Yabasic}}==
<langsyntaxhighlight lang=Yabasic>a = open("passwd", "a") // Open the file for appending, i.e. what you write to the file will be appended after its initial contents.
// If the file does not exist, it will be created.
Line 4,735:
wend
 
close #a</langsyntaxhighlight>
 
=={{header|zkl}}==
Line 4,752:
|}
{{trans|Ruby}}
<langsyntaxhighlight lang=zkl>var [const]
gnames=T("fullname","office","extension","homephone","email"),
pnames=T("account","password","uid","gid","gecos","directory","shell");
Line 4,765:
fcn init(str){ pnames.zipWith(setVar,vm.arglist) }
fcn toString { pnames.apply(setVar).concat(":") }
}</langsyntaxhighlight>
The class setVar method takes one or two parameters. With two, it sets the named class variable to a value; with one, it gets the var.
If there aren't enough parameters, the missing ones with be set to Void (yeah, poor error checking).
<langsyntaxhighlight lang=zkl>fcn strToPasswd(str){ // blow apart file line to class
p:=str.strip().split(":");
g:=Gecos(p[4].split(",").xplode());
Passwd(p[0,4].xplode(),g,p[5,*].xplode());
}</langsyntaxhighlight>
The List xplode method pushes the list contents to the parameter list.
<langsyntaxhighlight lang=zkl>jsmith:=Passwd("jsmith","x",1001, 1000,
Gecos("Joe Smith", "Room 1007", "(234)555-8917", "(234)555-0077", "jsmith@rosettacode.org"),
"/home/jsmith", "/bin/bash");
Line 4,789:
f.writeln(xyz); f.close();
 
File(filename).read().text.print(); // print file</langsyntaxhighlight>
{{out}}
<pre>
10,327

edits