Simple database: Difference between revisions

Content added Content deleted
(Undo revision 231640 by Gerard Schildberger (talk) Restored most of the page and attempted to put Gerard's changes back in...)
No edit summary
Line 2,688: Line 2,688:
watch_list(db);
watch_list(db);
}</lang>
}</lang>

=={{header|PowerShell}}==
<lang PowerShell>
function db
{
[CmdletBinding(DefaultParameterSetName="None")]
[OutputType([PSCustomObject])]
Param
(
[Parameter(Mandatory=$false,
Position=0,
ParameterSetName="Add a new entry")]
[string]
$Path = ".\SimpleDatabase.csv",

[Parameter(Mandatory=$true,
ParameterSetName="Add a new entry")]
[string]
$Name,

[Parameter(Mandatory=$true,
ParameterSetName="Add a new entry")]
[string]
$Category,

[Parameter(Mandatory=$true,
ParameterSetName="Add a new entry")]
[datetime]
$Birthday,

[Parameter(ParameterSetName="Print the latest entry")]
[switch]
$Latest,

[Parameter(ParameterSetName="Print the latest entry for each category")]
[switch]
$LatestByCategory,

[Parameter(ParameterSetName="Print all entries sorted by a date")]
[switch]
$SortedByDate
)

if (-not (Test-Path -Path $Path))
{
'"Name","Category","Birthday"' | Out-File -FilePath $Path
}

$db = Import-Csv -Path $Path | Foreach-Object {
$_.Birthday = $_.Birthday -as [datetime]
$_
}

switch ($PSCmdlet.ParameterSetName)
{
"Add a new entry"
{
[PSCustomObject]@{Name=$Name; Category=$Category; Birthday=$Birthday} | Export-Csv -Path $Path -Append
}
"Print the latest entry"
{
$db[-1]
}
"Print the latest entry for each category"
{
($db | Group-Object -Property Category).Name | ForEach-Object {($db | Where-Object -Property Category -Contains $_)[-1]}
}
"Print all entries sorted by a date"
{
$db | Sort-Object -Property Birthday
}
Default
{
$db
}
}
}

db -Name Bev -Category friend -Birthday 3/3/1983
db -Name Bob -Category family -Birthday 7/19/1987
db -Name Gill -Category friend -Birthday 12/9/1986
db -Name Gail -Category family -Birthday 2/11/1986
db -Name Vince -Category family -Birthday 3/10/1960
db -Name Wayne -Category coworker -Birthday 5/29/1962
</lang>
Here is the data from the CSV file as a PowerShell Object:
<lang PowerShell>
db
</lang>
{{Out}}
<pre>
Name Category Birthday
---- -------- --------
Bev friend 3/3/1983 12:00:00 AM
Bob family 7/19/1987 12:00:00 AM
Gill friend 12/9/1986 12:00:00 AM
Gail family 2/11/1986 12:00:00 AM
Vince family 3/10/1960 12:00:00 AM
Wayne coworker 5/29/1962 12:00:00 AM
</pre>
The latest entry:
<lang PowerShell>
db -Latest
</lang>
{{Out}}
<pre>
Name Category Birthday
---- -------- --------
Wayne coworker 5/29/1962 12:00:00 AM
</pre>
The latest entries by category:
<lang PowerShell>
db -LatestByCategory
</lang>
{{Out}}
<pre>
Name Category Birthday
---- -------- --------
Gill friend 12/9/1986 12:00:00 AM
Vince family 3/10/1960 12:00:00 AM
Wayne coworker 5/29/1962 12:00:00 AM
</pre>
The database sorted on the Birthday property:
<lang PowerShell>
db -SortedByDate
</lang>
{{Out}}
<pre>
Name Category Birthday
---- -------- --------
Vince family 3/10/1960 12:00:00 AM
Wayne coworker 5/29/1962 12:00:00 AM
Bev friend 3/3/1983 12:00:00 AM
Gail family 2/11/1986 12:00:00 AM
Gill friend 12/9/1986 12:00:00 AM
Bob family 7/19/1987 12:00:00 AM
</pre>


=={{header|Python}}==
=={{header|Python}}==