Starting a web browser
Build and show a web page displaying the data from the task separate the house number from the street name in a formatted and colored table.
- Task
Write the code which automatically opens a web page in a browser showing the addresses. No static data must be shown, only processed data.
EchoLisp
Since we are already in a browser window, the browser is started. print-hatml sends an html string to the standard output area. The table is styled with the "htable" css class, which is included in EchoLisp.
;; table from "Separate_the_house_number_from_the_street_name"
;;
(print-html (table->html adresses))
- Output:
Plataanstraat | 5 |
Straat | 12 |
Straat | 12 II |
Straat 1940 | II |
Dr. J. Straat | 40 |
Dr. J. Straat | 12 a |
Dr. J. Straat | 12-14 |
Laan 1940 – 1945 | 37 |
Plein 1940 | 2 |
1213-laan | 11 |
16 april | 1944 Pad 1 |
1e Kruisweg | 36 |
Laan 1940-'45 | 66 |
Laan '40-'45 | ❓❓❓ |
Langeloërduinen | 3 46 |
Marienwaerdt | 2e Dreef 2 |
Provincialeweg N205 | 1 |
Rivium | 2e Straat 59. |
Nieuwe gracht | 20rd |
Nieuwe gracht | 20rd 2 |
Nieuwe gracht | 20zw /2 |
Nieuwe gracht | 20zw/3 |
Nieuwe gracht | 20 zw/4 |
Bahnhofstr. | 4 |
Wertstr. | 10 |
Lindenhof | 1 |
Nordesch | 20 |
Weilstr. | 6 |
Harthauer Weg | 2 |
Mainaustr. | 49 |
August-Horch-Str. | 3 |
Marktplatz | 31 |
Schmidener Weg | 3 |
Karl-Weysser-Str. | 6 |
FreeBASIC
Type Row
Address As String
Street As String
House As String
Color As String
End Type
Function Split(Byref text As String, Byval delim As String, result() As String) As Integer
Dim As Integer index = 0, start = 1, posic
Redim result(0)
Do
posic = Instr(start, text, delim)
If posic = 0 Then posic = Len(text) + 1
Redim Preserve result(index)
result(index) = Mid(text, start, posic - start)
index += 1
start = posic + Len(delim)
Loop Until start > Len(text)
Return index
End Function
Function Replace(Byref src As String, Byref find As String, Byref repl As String) As String
Dim As String result = src
Dim As Integer posic = Instr(result, find)
While posic > 0
result = Left(result, posic - 1) & repl & Mid(result, posic + Len(find))
posic = Instr(posic + Len(repl), result, find)
Wend
Return result
End Function
Function isDigit(Byval ch As String) As Boolean
Return (Asc(ch) >= Asc("0") And Asc(ch) <= Asc("9"))
End Function
Sub SeparateHouseNumber(address As String, Byref street As String, Byref house As String)
Dim As String fields()
Dim As Integer size = 0
Split(address, " ", fields())
size = Ubound(fields) + 1
Dim As String last = fields(size - 1)
Dim As String penult = fields(size - 2)
If isDigit(Left(last, 1)) Then
Dim As Boolean isdig = isDigit(Left(penult, 1))
If size > 2 And isdig And Not Left(penult, 3) = "194" Then
house = penult & " " & last
Else
house = last
End If
Elseif size > 2 Then
house = penult & " " & last
End If
street = Rtrim(Left(address, Len(address) - Len(house)))
End Sub
Dim Shared As String tmpl
tmpl = _
"<!DOCTYPE html>" & _
"<html>" & _
"<head>" & _
" <title>Rosetta Code - Start a Web Browser</title>" & _
" <meta charset=""UTF-8"">" & _
"</head>" & _
"<body bgcolor=""#d8dcd6"">" & _
" <table border=""2"">" & _
" <p align=""center"">" & _
" <font face=""Arial, sans-serif"" size=""5"">Split the house number from the street name</font>" & _
" <tr bgcolor=""#02ccfe""><th>Address</th><th>Street</th><th>House Number</th></tr>" & _
" {{ROWS}}" & _
" </p>" & _
" </table>" & _
"</body>" & _
"</html>"
Dim As String addresses(32) = { _
"Plataanstraat 5", _
"Straat 12", _
"Straat 12 II", _
"Dr. J. Straat 12", _
"Dr. J. Straat 12 a", _
"Dr. J. Straat 12-14", _
"Laan 1940 - 1945 37", _
"Plein 1940 2", _
"1213-laan 11", _
"16 april 1944 Pad 1", _
"1e Kruisweg 36", _
"Laan 1940-'45 66", _
"Laan '40-'45", _
"Langeloërduinen 3 46", _
"Marienwaerdt 2e Dreef 2", _
"Provincialeweg N205 1", _
"Rivium 2e Straat 59.", _
"Nieuwe gracht 20rd", _
"Nieuwe gracht 20rd 2", _
"Nieuwe gracht 20zw /2", _
"Nieuwe gracht 20zw/3", _
"Nieuwe gracht 20 zw/4", _
"Bahnhofstr. 4", _
"Wertstr. 10", _
"Lindenhof 1", _
"Nordesch 20", _
"Weilstr. 6", _
"Harthauer Weg 2", _
"Mainaustr. 49", _
"August-Horch-Str. 3", _
"Marktplatz 31", _
"Schmidener Weg 3", _
"Karl-Weysser-Str. 6" }
Dim As String browser = "start" ' For Windows
' Or alternatively for cross-platform
'Dim As String browser = "xdg-open" ' For Linux, xBSD
Dim As String colors(1) = {"#d7fffe", "#9dbcd4"}
Dim As String fileName = "addresses_table.html"
Dim As Integer ff = Freefile()
Dim As Row rows(Ubound(addresses))
Dim As String rowsHtml = ""
For i As Integer = 0 To Ubound(addresses)
Dim As String street, house
SeparateHouseNumber(addresses(i), street, house)
If house = "" Then house = "(none)"
rows(i).Address = addresses(i)
rows(i).Street = street
rows(i).House = house
rows(i).Color = colors(i Mod 2)
rowsHtml &= "<tr bgcolor=""" & rows(i).Color & """>" & _
"<td>" & rows(i).Address & "</td>" & _
"<td>" & rows(i).Street & "</td>" & _
"<td>" & rows(i).House & "</td>" & _
"</tr>" & Chr(10)
Next i
tmpl = Replace(tmpl, "{{ROWS}}", rowsHtml)
Open fileName For Output As #ff
Print #ff, tmpl
Close #ff
Shell(browser & " " & fileName)
Sleep 5000 ' wait for 5 seconds before deleting file
Kill(fileName)
Sleep
- Output:
Similar to the Raku entry.
Go
This uses the same format and color scheme for the table as the Raku entry.
package main
import (
"fmt"
"html/template"
"log"
"os"
"os/exec"
"strings"
"time"
)
type row struct {
Address, Street, House, Color string
}
func isDigit(b byte) bool {
return '0' <= b && b <= '9'
}
func separateHouseNumber(address string) (street string, house string) {
length := len(address)
fields := strings.Fields(address)
size := len(fields)
last := fields[size-1]
penult := fields[size-2]
if isDigit(last[0]) {
isdig := isDigit(penult[0])
if size > 2 && isdig && !strings.HasPrefix(penult, "194") {
house = fmt.Sprintf("%s %s", penult, last)
} else {
house = last
}
} else if size > 2 {
house = fmt.Sprintf("%s %s", penult, last)
}
street = strings.TrimRight(address[:length-len(house)], " ")
return
}
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
var tmpl = `
<head>
<title>Rosetta Code - Start a Web Browser</title>
<meta charset="UTF-8">
</head>
<body bgcolor="#d8dcd6">
<table border="2">
<p align="center">
<font face="Arial, sans-serif" size="5">Split the house number from the street name</font>
<tr bgcolor="#02ccfe"><th>Address</th><th>Street</th><th>House Number</th></tr>
{{range $row := .}}
<tr bgcolor={{$row.Color}}>
<td>{{$row.Address}}</td>
<td>{{$row.Street}}</td>
<td>{{$row.House}}</td>
</tr>
{{end}}
</p>
</table>
</body>
`
func main() {
addresses := []string{
"Plataanstraat 5",
"Straat 12",
"Straat 12 II",
"Dr. J. Straat 12",
"Dr. J. Straat 12 a",
"Dr. J. Straat 12-14",
"Laan 1940 - 1945 37",
"Plein 1940 2",
"1213-laan 11",
"16 april 1944 Pad 1",
"1e Kruisweg 36",
"Laan 1940-'45 66",
"Laan '40-'45",
"Langeloërduinen 3 46",
"Marienwaerdt 2e Dreef 2",
"Provincialeweg N205 1",
"Rivium 2e Straat 59.",
"Nieuwe gracht 20rd",
"Nieuwe gracht 20rd 2",
"Nieuwe gracht 20zw /2",
"Nieuwe gracht 20zw/3",
"Nieuwe gracht 20 zw/4",
"Bahnhofstr. 4",
"Wertstr. 10",
"Lindenhof 1",
"Nordesch 20",
"Weilstr. 6",
"Harthauer Weg 2",
"Mainaustr. 49",
"August-Horch-Str. 3",
"Marktplatz 31",
"Schmidener Weg 3",
"Karl-Weysser-Str. 6",
}
browser := "firefox" // or whatever
colors := [2]string{"#d7fffe", "#9dbcd4"}
fileName := "addresses_table.html"
ct := template.Must(template.New("").Parse(tmpl))
file, err := os.Create(fileName)
check(err)
rows := make([]row, len(addresses))
for i, address := range addresses {
street, house := separateHouseNumber(address)
if house == "" {
house = "(none)"
}
color := colors[i%2]
rows[i] = row{address, street, house, color}
}
err = ct.Execute(file, rows)
check(err)
cmd := exec.Command(browser, fileName)
err = cmd.Run()
check(err)
file.Close()
time.Sleep(5 * time.Second) // wait for 5 seconds before deleting file
err = os.Remove(fileName)
check(err)
}
- Output:
Similar to the Raku entry.
J
Here, we assume that browser
refers to a browser in a format which is usable by a command shell, and din5008
and sampledata
have been defined according to the J house number street name task entry:
wrap=: {{'<',m,'>',y,'</',(;{.;:m),'>' }}(L:0)("1)
'table.htm'fwrite~'<style>tr:nth-child(even){color:blue}</style>','table' wrap ,'tr' wrap ;"1 'td' wrap din5008"1 sampledata
1660
launch browser,' table.htm'
(Here, 1660 was the length of the generated file.)
Java
import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.TimeUnit;
public final class StartingAWebBrowser {
public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException {
List<String> addresses = List.of(
"Plataanstraat 5",
"Straat 12",
"Straat 12 II",
"Dr. J. Straat 12",
"Dr. J. Straat 12 a",
"Dr. J. Straat 12-14",
"Laan 1940 - 1945 37",
"Plein 1940 2",
"1213-laan 11",
"16 april 1944 Pad 1",
"1e Kruisweg 36",
"Laan 1940-'45 66",
"Laan '40-'45",
"Langeloërduinen 3 46",
"Marienwaerdt 2e Dreef 2",
"Provincialeweg N205 1",
"Rivium 2e Straat 59.",
"Nieuwe gracht 20rd",
"Nieuwe gracht 20rd 2",
"Nieuwe gracht 20zw /2",
"Nieuwe gracht 20zw/3",
"Nieuwe gracht 20 zw/4",
"Bahnhofstr. 4",
"Wertstr. 10",
"Lindenhof 1",
"Nordesch 20",
"Weilstr. 6",
"Harthauer Weg 2",
"Mainaustr. 49",
"August-Horch-Str. 3",
"Marktplatz 31",
"Schmidener Weg 3",
"Karl-Weysser-Str. 6"
);
String htmlHeader = """
<html>
<head>
<title>Rosetta Code - Start a Web Browser</title>
<meta charset="UTF-8">
</head>
<body bgcolor="#e6e6ff">
<p align="center">
<font face="Arial, sans-serif" size="5">Split the house number from the street name</font>
</p>
<p align="center">
<table border="2"> <tr bgcolor="#9bbb59">
<th>Address</th><th>Street</th><th>House Number</th>
""";
String htmlFooter = """
</table>
</p>
</body>
</html>
""";
StringBuilder builder = new StringBuilder(htmlHeader);
for ( int i = 0; i < addresses.size(); i++ ) {
String colour = ( i % 2 == 0 ) ? "#d8e4bc" : "#ebf1de";
Tuple result = separateStreetAndHouse(addresses.get(i));
builder.append("<tr bgcolor=" + colour + "><td>" + addresses.get(i) + "</td><td>"
+ result.street + "</td><td>" + result.house + "</td></tr>" + System.lineSeparator());
}
builder.append(htmlFooter);
String filePath = "test.html";
Files.write(Paths.get(filePath), builder.toString().getBytes());
openBrowser(filePath);
TimeUnit.SECONDS.sleep(5);
Files.delete(Paths.get(filePath));
}
private static void openBrowser(String url) throws IOException, URISyntaxException {
if ( Desktop.isDesktopSupported() ) {
Desktop desktop = Desktop.getDesktop();
desktop.browse( new URI(url) );
} else {
Runtime runtime = Runtime.getRuntime();
runtime.exec( new String[] { "xdg-open " + url, null, null } );
}
}
private static Tuple separateStreetAndHouse(String address) {
String house = "";
String[] fields = address.split("\\s+");
String last = fields[fields.length - 1];
String penultimate = fields[fields.length - 2];
if ( Character.isDigit(last.charAt(0) ) ) {
final boolean isDigit = Character.isDigit(penultimate.charAt(0));
if ( fields.length > 2 && isDigit && ! penultimate.startsWith("194") ) {
house = penultimate + " " + last;
} else {
house = last;
}
} else if ( fields.length > 2 ) {
house = penultimate + " " + last;
}
String street = address.substring(0, address.length() - house.length()).stripTrailing();
return new Tuple(street, house);
}
private static record Tuple(String street, String house) {}
}
- Output:
Julia
using Tables, BrowseTables
function testbrowsertable(addresstext)
lines = strip.(split(addresstext, "\n"))
mat = fill("", length(lines), 2)
regex = r"""^ (.*?) \s+
(
\d* (\-|\/)? \d*
| \d{1,3} [a-zI./ ]* \d{0,3}
)
$"""x
for (i, line) in enumerate(lines)
if (matched = match(regex, line)) != nothing
mat[i, 1], mat[i, 2] = matched.captures
end
end
data = Tables.table(mat)
tmp = tempname() * ".html"
write_html_table(tmp, data)
if Sys.isapple()
run(`open $tmp`)
elseif Sys.iswindows()
run(`cmd /c start $tmp`)
else # linux etc.
run(`xdg-open $tmp`)
end
println("Press Enter after you close the browser to exit and remove temp file.")
readline()
rm(tmp)
end
const adressen = """
Plataanstraat 5
Straat 12
Straat 12 II
Dr. J. Straat 12
Dr. J. Straat 12 a
Dr. J. Straat 12-14
Laan 1940 – 1945 37
Plein 1940 2
1213-laan 11
16 april 1944 Pad 1
1e Kruisweg 36
Laan 1940-’45 66
Laan ’40-’45
Langeloërduinen 3 46
Marienwaerdt 2e Dreef 2
Provincialeweg N205 1
Rivium 2e Straat 59.
Nieuwe gracht 20rd
Nieuwe gracht 20rd 2
Nieuwe gracht 20zw /2
Nieuwe gracht 20zw/3
Nieuwe gracht 20 zw/4
Bahnhofstr. 4
Wertstr. 10
Lindenhof 1
Nordesch 20
Weilstr. 6
Harthauer Weg 2
Mainaustr. 49
August-Horch-Str. 3
Marktplatz 31
Schmidener Weg 3
Karl-Weysser-Str. 6"""
testbrowsertable(adressen)
Nim
Based on Phix solution. Using same colors as Scala program.
import os, strutils
func separateHouseNumber(address: string): tuple[street, house: string] =
let fields = address.splitWhitespace()
let last = fields[^1]
let penult = fields[^2]
if last[0].isDigit():
let isdig = penult[0].isDigit()
if fields.len > 2 and isdig and not penult.startsWith("194"):
result.house = penult & ' ' & last
else:
result.house = last
elif fields.len > 2:
result.house = penult & ' ' & last
result.street = address[0..address.high-result.house.len].strip(leading = false, trailing = true)
const
Addresses = ["Plataanstraat 5",
"Straat 12",
"Straat 12 II",
"Dr. J. Straat 12",
"Dr. J. Straat 12 a",
"Dr. J. Straat 12-14",
"Laan 1940 - 1945 37",
"Plein 1940 2",
"1213-laan 11",
"16 april 1944 Pad 1",
"1e Kruisweg 36",
"Laan 1940-'45 66",
"Laan '40-'45",
"Langeloërduinen 3 46",
"Marienwaerdt 2e Dreef 2",
"Provincialeweg N205 1",
"Rivium 2e Straat 59.",
"Nieuwe gracht 20rd",
"Nieuwe gracht 20rd 2",
"Nieuwe gracht 20zw /2",
"Nieuwe gracht 20zw/3",
"Nieuwe gracht 20 zw/4",
"Bahnhofstr. 4",
"Wertstr. 10",
"Lindenhof 1",
"Nordesch 20",
"Weilstr. 6",
"Harthauer Weg 2",
"Mainaustr. 49",
"August-Horch-Str. 3",
"Marktplatz 31",
"Schmidener Weg 3",
"Karl-Weysser-Str. 6"]
HtmlHeader = """
<html>
<head>
<title>Rosetta Code - Start a Web Browser</title>
<meta charset="UTF-8">
</head>
<body bgcolor="#e6e6ff">
<p align="center">
<font face="Arial, sans-serif" size="5">Split the house number from the street name</font>
</p>
<p align="center">
<table border="2"> <tr bgcolor="#9bbb59">
<th>Address</th><th>Street</th><th>House Number</th>
"""
HtmlLine = "<tr bgcolor=$#><td>$#</td><td>$#</td><td>$#</td></tr>"
HtmlFooter = """
</table>
</p>
</body>
</html>
"""
let htmlFile = open("test.html", fmWrite)
htmlFile.write HtmlHeader
for i, address in Addresses:
let color = if (i and 1) == 0: "#ebf1de" else: "#d8e4bc"
let (street, house) = address.separateHouseNumber()
htmlFile.write HtmlLine.format(color, address, street, house)
htmlFile.write HtmlFooter
htmlFile.close()
discard execShellCmd("firefox test.html")
os.sleep(5000)
removeFile("test.html")
- Output:
Same as Phix with Scala colors.
Perl
Borrowing code from the Separate the house number from the street name task.
use File::Temp qw(tempfile);
my @addresses = (
'Plataanstraat 5', 'Straat 12', 'Straat 12 II', 'Dr. J. Straat 12',
'Dr. J. Straat 12 a', 'Dr. J. Straat 12-14', 'Laan 1940 – 1945 37', 'Plein 1940 2',
'1213-laan 11', '16 april 1944 Pad 1', '1e Kruisweg 36', 'Laan 1940-’45 66',
'Laan ’40-’45', 'Langeloërduinen 3 46', 'Marienwaerdt 2e Dreef 2', 'Provincialeweg N205 1',
'Rivium 2e Straat 59.', 'Nieuwe gracht 20rd', 'Nieuwe gracht 20rd 2', 'Nieuwe gracht 20zw /2',
'Nieuwe gracht 20zw/3', 'Nieuwe gracht 20 zw/4', 'Bahnhofstr. 4', 'Wertstr. 10',
'Lindenhof 1', 'Nordesch 20', 'Weilstr. 6', 'Harthauer Weg 2',
'Mainaustr. 49', 'August-Horch-Str. 3', 'Marktplatz 31', 'Schmidener Weg 3',
'Karl-Weysser-Str. 6');
my @row_color = ('#d7fffe', '#9dbcd4');
# build the table
sub genTable {
my $table = '<table border="2"> <tr bgcolor="#02ccfe">' .
qq|<th>Address</th><th>Street</th><th>House Number</th>\n|;
my $i = 0;
for my $addr (@addresses) {
$table .= qq|<tr bgcolor="@{[@row_color[$i++ % 2]]}"><td>$addr</td>|;
my($street,$number) = $addr =~
m[^ (.*?) \s+
(
\d* (\-|\/)? \d*
| \d{1,3} [a-zI./ ]* \d{0,3}
) $
]x;
if (!$number) { $street = $addr; $number = '(no match)' }
$table .= qq|<td>$street</td><td>$number</td></tr>\n|;
}
return $table . '</table>';
}
my $streets_and_numbers = genTable();
# generate the page content
sub content {
return <<END;
<html>
<head>
<title>Rosetta Code - Start a Web Browser</title>
<meta charset="UTF-8">
</head>
<body bgcolor="#d8dcd6">
<p align="center">
<font face="Arial, sans-serif" size="5">Split the house number from the street name</font>
</p>
<p align="center">
$streets_and_numbers
</p>
</body>
</html>
END
}
# Use a temporary file name and file handle
my ($fn, $fh) = tempfile :suffix('.html');
# dump the content to the file
open my $fh, '>', $fn;
print $fh content();
close $fh;
# use appropriate command for X11 (other systems will need different invocation)
my $command = "xdg-open $fn";
# start the browser
system "$command";
# wait for a bit to give browser time to load before destroying temp file
sleep 5;
- Output:
Address | Street | House Number |
---|---|---|
Plataanstraat 5 | Plataanstraat | 5 |
Straat 12 | Straat | 12 |
Straat 12 II | Straat | 12 II |
Dr. J. Straat 12 | Dr. J. Straat | 12 |
Dr. J. Straat 12 a | Dr. J. Straat | 12 a |
Dr. J. Straat 12-14 | Dr. J. Straat | 12-14 |
Laan 1940 – 1945 37 | Laan 1940 – 1945 | 37 |
Plein 1940 2 | Plein 1940 | 2 |
1213-laan 11 | 1213-laan | 11 |
16 april 1944 Pad 1 | 16 april 1944 Pad | 1 |
1e Kruisweg 36 | 1e Kruisweg | 36 |
Laan 1940-’45 66 | Laan 1940-’45 | 66 |
Laan ’40-’45 | Laan ’40-’45 | (no match) |
Langeloërduinen 3 46 | Langeloërduinen | 3 46 |
Marienwaerdt 2e Dreef 2 | Marienwaerdt 2e Dreef | 2 |
Provincialeweg N205 1 | Provincialeweg N205 | 1 |
Rivium 2e Straat 59. | Rivium 2e Straat | 59. |
Nieuwe gracht 20rd | Nieuwe gracht | 20rd |
Nieuwe gracht 20rd 2 | Nieuwe gracht | 20rd 2 |
Nieuwe gracht 20zw /2 | Nieuwe gracht | 20zw /2 |
Nieuwe gracht 20zw/3 | Nieuwe gracht | 20zw/3 |
Nieuwe gracht 20 zw/4 | Nieuwe gracht | 20 zw/4 |
Bahnhofstr. 4 | Bahnhofstr. | 4 |
Wertstr. 10 | Wertstr. | 10 |
Lindenhof 1 | Lindenhof | 1 |
Nordesch 20 | Nordesch | 20 |
Weilstr. 6 | Weilstr. | 6 |
Harthauer Weg 2 | Harthauer Weg | 2 |
Mainaustr. 49 | Mainaustr. | 49 |
August-Horch-Str. 3 | August-Horch-Str. | 3 |
Marktplatz 31 | Marktplatz | 31 |
Schmidener Weg 3 | Schmidener Weg | 3 |
Karl-Weysser-Str. 6 | Karl-Weysser-Str. | 6 |
Phix
without js -- (file i/o, system) constant addresses = {"Plataanstraat 5", "Straat 12", "Straat 12 II", "Dr. J. Straat 12", "Dr. J. Straat 12 a", "Dr. J. Straat 12-14", "Laan 1940 - 1945 37", "Plein 1940 2", "1213-laan 11", "16 april 1944 Pad 1", "1e Kruisweg 36", "Laan 1940-'45 66", "Laan '40-'45", "Langeloërduinen 3 46", "Marienwaerdt 2e Dreef 2", "Provincialeweg N205 1", "Rivium 2e Straat 59.", "Nieuwe gracht 20rd", "Nieuwe gracht 20rd 2", "Nieuwe gracht 20zw /2", "Nieuwe gracht 20zw/3", "Nieuwe gracht 20 zw/4", "Bahnhofstr. 4", "Wertstr. 10", "Lindenhof 1", "Nordesch 20", "Weilstr. 6", "Harthauer Weg 2", "Mainaustr. 49", "August-Horch-Str. 3", "Marktplatz 31", "Schmidener Weg 3", "Karl-Weysser-Str. 6"} function isDigit(integer ch) return ch>='0' and ch<='9' end function function separateHouseNumber(integer i) string address = addresses[i] sequence parts = split(address) string street, house integer h = 0 if length(parts)>1 then string last = parts[$] if isDigit(last[1]) then h = 1 string penult = parts[$-1] if length(parts)>2 and isDigit(penult[1]) and match("194",penult)!=1 then h = 2 end if elsif length(parts)>2 then h = 2 end if end if if h then street = join(parts[1..$-h]) house = join(parts[$-h+1..$]) else street = join(parts) house = "(none)" end if string colour = iff(mod(i,2)=0?"#d7fffe":"#9dbcd4") return {colour,address,street,house} end function constant html_hdr = """ <html> <head> <title>Rosetta Code - Start a Web Browser</title> <meta charset="UTF-8"> </head> <body bgcolor="#d8dcd6"> <p align="center"> <font face="Arial, sans-serif" size="5">Split the house number from the street name</font> </p> <p align="center"> <table border="2"> <tr bgcolor="#02ccfe"> <th>Address</th><th>Street</th><th>House Number</th> """, html_line = """ <tr bgcolor=%s><td>%s</td><td>%s</td><td>%s</td></tr> """, html_ftr = """ </table> </p> </body> </html> """ procedure main() integer fn = open("test.html","w") printf(fn,html_hdr) for i=1 to length(addresses) do printf(fn,html_line,separateHouseNumber(i)) end for printf(fn,html_ftr) close(fn) system("test.html") end procedure main()
output as perl
Python
import os
import re
import time
import webbrowser
ADDRESSES = [
"Plataanstraat 5",
"Straat 12",
"Straat 12 II",
"Dr. J. Straat 12",
"Dr. J. Straat 12 a",
"Dr. J. Straat 12-14",
"Laan 1940 – 1945 37",
"Plein 1940 2",
"1213-laan 11",
"16 april 1944 Pad 1",
"1e Kruisweg 36",
"Laan 1940-’45 66",
"Laan ’40-’45",
"Langeloërduinen 3 46",
"Marienwaerdt 2e Dreef 2",
"Provincialeweg N205 1",
"Rivium 2e Straat 59.",
"Nieuwe gracht 20rd",
"Nieuwe gracht 20rd 2",
"Nieuwe gracht 20zw /2",
"Nieuwe gracht 20zw/3",
"Nieuwe gracht 20 zw/4",
"Bahnhofstr. 4",
"Wertstr. 10",
"Lindenhof 1",
"Nordesch 20",
"Weilstr. 6",
"Harthauer Weg 2",
"Mainaustr. 49",
"August-Horch-Str. 3",
"Marktplatz 31",
"Schmidener Weg 3",
"Karl-Weysser-Str. 6",
]
def split_address(address: str):
arr = [
item.strip()
for item in re.split(
r"(\s\d+[-/]\d+)|(\s(?!1940|1945)\d+[a-zI. /]*\d*)$|\d+\['][40|45]$",
address,
)
if item is not None and item != ""
]
if len(arr) < 2:
return arr[0], ""
return arr
def main():
HTML_HEADER = """
<html>
<head>
<title>Rosetta Code - Start a Web Browser</title>
<meta charset="UTF-8">
</head>
<body bgcolor="#e6e6ff">
<p align="center">
<font face="Arial, sans-serif" size="5">Split the house number from the street name</font>
</p>
<p align="center">
<table border="2"> <tr bgcolor="#9bbb59">
<th>Address</th><th>Street</th><th>House Number</th>
"""
HTML_FOOTER = """
</table>
</p>
</body>
</html>
"""
HTML_LINE = "<tr bgcolor={0}><td>{1}</td><td>{2}</td><td>{3}</td></tr>"
with open("index.html", "w") as html_file:
html_file.write(HTML_HEADER)
for i, addr in enumerate(ADDRESSES):
color = "#ebf1de" if i & 1 == 0 else "#d8e4bc"
street, number = split_address(addr)
html_file.write(HTML_LINE.format(color, addr, street, number))
html_file.write(HTML_FOOTER)
webbrowser.open("index.html")
time.sleep(5)
os.remove("index.html")
if __name__ == "__main__":
main()
- Output:
Same as Phix with Scala colors.
Racket
#lang at-exp racket
... same code as "Separate_the_house_number_from_the_street_name" ...
(require net/sendurl scribble/html)
(define (render-table)
(for/list ([str (in-list (string-split adressen #rx" *\r?\n *"))]
[i (in-naturals)])
(tr bgcolor: (if (even? i) "#fcf" "#cff")
(td str)
(map td (cond [(splits-adressen str) => cdr] [else '(??? ???)])))))
@(compose1 send-url/contents xml->string){
@html{@head{@title{Splitting Results}}
@body{@h1{Splitting Results}
@table{@render-table}}}
}
Raku
(formerly Perl 6)
Uses the code from the Separate the house number from the street name task almost verbatim. Included here to make a complete, runnable example.
use File::Temp;
my $addresses = qq :to /END/;
Plataanstraat 5
Straat 12
Straat 12 II
Dr. J. Straat 12
Dr. J. Straat 12 a
Dr. J. Straat 12-14
Laan 1940 – 1945 37
Plein 1940 2
1213-laan 11
16 april 1944 Pad 1
1e Kruisweg 36
Laan 1940-’45 66
Laan ’40-’45
Langeloërduinen 3 46
Marienwaerdt 2e Dreef 2
Provincialeweg N205 1
Rivium 2e Straat 59.
Nieuwe gracht 20rd
Nieuwe gracht 20rd 2
Nieuwe gracht 20zw /2
Nieuwe gracht 20zw/3
Nieuwe gracht 20 zw/4
Bahnhofstr. 4
Wertstr. 10
Lindenhof 1
Nordesch 20
Weilstr. 6
Harthauer Weg 2
Mainaustr. 49
August-Horch-Str. 3
Marktplatz 31
Schmidener Weg 3
Karl-Weysser-Str. 6
END
my @row-color = '#d7fffe', '#9dbcd4';
# build the table
sub genTable () {
my $table = '<table border="2"> <tr bgcolor="#02ccfe">' ~
qq|<th>Address</th><th>Street</th><th>House Number</th>\n|;
my $i = 0;
for $addresses.lines -> $addr {
$table ~= qq|<tr bgcolor="{@row-color[$i++ % 2]}"><td>{$addr}</td>|;
$addr ~~ m[
( .*? )
[
\s+
(
| \d+ [ \- | \/ ] \d+
| <!before 1940 | 1945> \d+ <[ a..z I . / \x20 ]>* \d*
)
]?
$
];
quietly $table ~= qq|<td>{$0.Str}</td><td>{$1.Str||''}</td></tr>\n|;
}
$table ~ '</table>';
}
# generate the page content
sub content {
qq :to /END/;
<html>
<head>
<title>Rosetta Code - Start a Web Browser</title>
<meta charset="UTF-8">
</head>
<body bgcolor="#d8dcd6">
<p align="center">
<font face="Arial, sans-serif" size="5">Split the house number from the street name</font>
</p>
<p align="center">
{ genTable }
</p>
</body>
</html>
END
}
# Use a temporary file name and file handle
my ($fn, $fh) = tempfile :suffix('.html');
# dump the content to the file
$fh.spurt: content;
# use appropriate command for Windows or X11
# other OSs/WMs may need different invocation
my $command = $*DISTRO.is-win ?? "start $fn" !! "xdg-open $fn";
# start the browser
shell $command;
# wait for a bit to give browser time to load before destroying temp file
sleep 5;
- Output:
Will start the default browser (or open a new tab/window in a running one) and display this table.
Address | Street | House Number |
---|---|---|
Plataanstraat 5 | Plataanstraat | 5 |
Straat 12 | Straat | 12 |
Straat 12 II | Straat | 12 II |
Dr. J. Straat 12 | Dr. J. Straat | 12 |
Dr. J. Straat 12 a | Dr. J. Straat | 12 a |
Dr. J. Straat 12-14 | Dr. J. Straat | 12-14 |
Laan 1940 – 1945 37 | Laan 1940 – 1945 | 37 |
Plein 1940 2 | Plein 1940 | 2 |
1213-laan 11 | 1213-laan | 11 |
16 april 1944 Pad 1 | 16 april 1944 Pad | 1 |
1e Kruisweg 36 | 1e Kruisweg | 36 |
Laan 1940-’45 66 | Laan 1940-’45 | 66 |
Laan ’40-’45 | Laan ’40-’45 | |
Langeloërduinen 3 46 | Langeloërduinen | 3 46 |
Marienwaerdt 2e Dreef 2 | Marienwaerdt 2e Dreef | 2 |
Provincialeweg N205 1 | Provincialeweg N205 | 1 |
Rivium 2e Straat 59. | Rivium 2e Straat | 59. |
Nieuwe gracht 20rd | Nieuwe gracht | 20rd |
Nieuwe gracht 20rd 2 | Nieuwe gracht | 20rd 2 |
Nieuwe gracht 20zw /2 | Nieuwe gracht | 20zw /2 |
Nieuwe gracht 20zw/3 | Nieuwe gracht | 20zw/3 |
Nieuwe gracht 20 zw/4 | Nieuwe gracht | 20 zw/4 |
Bahnhofstr. 4 | Bahnhofstr. | 4 |
Wertstr. 10 | Wertstr. | 10 |
Lindenhof 1 | Lindenhof | 1 |
Nordesch 20 | Nordesch | 20 |
Weilstr. 6 | Weilstr. | 6 |
Harthauer Weg 2 | Harthauer Weg | 2 |
Mainaustr. 49 | Mainaustr. | 49 |
August-Horch-Str. 3 | August-Horch-Str. | 3 |
Marktplatz 31 | Marktplatz | 31 |
Schmidener Weg 3 | Schmidener Weg | 3 |
Karl-Weysser-Str. 6 | Karl-Weysser-Str. | 6 |
Scala
Uses nothing but the Standard Library:
import java.awt.Desktop
import java.io.{IOException, PrintWriter}
import java.net.{URI, ServerSocket}
import scala.xml.Elem
class WebServer(port: Int, soleDocument: Elem) extends Thread {
this.setName(s"Server at $port")
override def run() {
val listener = try {
new ServerSocket(port)
} catch {
case e: java.net.BindException => throw new IllegalStateException(s"Port $port already taken!")
}
println(s"Listening on port ${listener.getLocalPort}")
while (!Thread.interrupted()) {
try {
//print(".")
val socket = listener.accept
new PrintWriter(socket.getOutputStream, true).println(soleDocument)
socket.close()
} catch {
case ioe: IOException => println(ioe)
}
}
}
} // class WebServer
object HtmlServer extends App {
val PORT = 64507
// Main
val thread = new WebServer(PORT, HtmlBuilder)
val uri = URI.create(s"http://localhost:$PORT/")
thread.start()
def HtmlBuilder: Elem = {
def adressen: Iterator[String] =
"""Plataanstraat 5
|Straat 12
|Straat 12 II
|Straat 1940 II
|Dr. J. Straat 40
|Dr. J. Straat 12 a
|Dr. J. Straat 12-14
|Laan 1940 – 1945 37
|Plein 1940 2
|1213-laan 11
|16 april 1944 Pad 1
|1e Kruisweg 36
|Laan 1940-’45 66
|Laan ’40-’45
|Langeloërduinen 3 46
|Marienwaerdt 2e Dreef 2
|Provincialeweg N205 1
|Rivium 2e Straat 59.
|Nieuwe gracht 20rd
|Nieuwe gracht 20rd 2
|Nieuwe gracht 20zw /2
|Nieuwe gracht 20zw/3
|Nieuwe gracht 20 zw/4
|Bahnhofstr. 4
|Wertstr. 10
|Lindenhof 1
|Nordesch 20
|Weilstr. 6
|Harthauer Weg 2
|Mainaustr. 49
|August-Horch-Str. 3
|Marktplatz 31
|Schmidener Weg 3
|Karl-Weysser-Str. 6""".stripMargin.lines
def getSplittedAddresses(addresses: Iterator[String]) = {
val extractor = new scala.util.matching.Regex( """(\s\d+[-/]\d+)|(\s(?!1940|1945)\d+[a-zI. /]*\d*)$|\d+\['][40|45]$""")
def splitsAdres(input: String): (String, String) =
(extractor.split(input).mkString, extractor.findFirstIn(input).getOrElse(""))
addresses.map(org => {
val temp = splitsAdres(org)
List(org, temp._1, temp._2)
})
}
def generateTable: Elem = {
def coloring(rownum: Any): String = {
rownum match {
case Nil => "#9bbb59"
case n: Int => if (n % 2 == 0) "#ebf1de" else "#d8e4bc"
}
}
<table border="10">
{(List(List("Given Address", "Street", "House Number")) ++ getSplittedAddresses(adressen)).
zipWithIndex.map { case (row, rownum) => (if (rownum == 0) Nil else rownum) +: row}.map(row =>
<tr bgcolor={coloring(row.head)}>
{row.map(cell =>
if (row.head == Nil)
<th>
{cell}
</th>
else
<td>
{cell}
</td>)}
</tr>)}
</table>
} // def generateTable
<html>
<head>
<title>Rosetta.org Task solution</title>
</head>
<body lang="en-US" bgcolor="#e6e6ff" dir="LTR">
<p align="CENTER">
<font face="Arial, sans-serif" size="5">Split the house number from the street name</font>
</p>
<p align="CENTER">
{generateTable}
</p>
</body>
</html>
} // def content
if (Desktop.isDesktopSupported && Desktop.getDesktop.isSupported(Desktop.Action.BROWSE))
Desktop.getDesktop.browse(uri)
else println(s"Automatic start of Web browser not possible.\nWeb browser must be started manually, use $uri.")
if (!thread.isAlive) sys.exit(-1)
println("Web server started.")
do print("Do you want to shutdown this server? <Y(es)/N>: ") while (!scala.io.StdIn.readBoolean)
sys.exit()
}
- Output:
Split the house number from the street name
Given Address | Street | House Number | |
---|---|---|---|
1 | Plataanstraat 5 | Plataanstraat | 5 |
2 | Straat 12 | Straat | 12 |
3 | Straat 12 II | Straat | 12 II |
4 | Straat 1940 II | Straat 1940 II | |
5 | Dr. J. Straat40 | Dr. J. Straat | 40 |
6 | Dr. J. Straat 12 a | Dr. J. Straat | 12 a |
7 | Dr. J. Straat 12-14 | Dr. J. Straat | 12-14 |
8 | Laan 1940 – 1945 37 | Laan 1940 – 1945 | 37 |
9 | Plein 1940 2 | Plein 1940 | 2 |
10 | 1213-laan 11 | 1213-laan | 11 |
11 | 16 april 1944 Pad 1 | 16 april 1944 Pad | 1 |
12 | 1e Kruisweg 36 | 1e Kruisweg | 36 |
13 | Laan 1940-’45 66 | Laan 1940-’45 | 66 |
14 | Laan ’40-’45 | Laan ’40-’45 | |
15 | Langeloërduinen 3 46 | Langeloërduinen | 3 46 |
16 | Marienwaerdt 2e Dreef 2 | Marienwaerdt 2e Dreef | 2 |
17 | Provincialeweg N205 1 | Provincialeweg N205 | 1 |
18 | Rivium 2e Straat 59. | Rivium 2e Straat | 59. |
19 | Nieuwe gracht 20rd | Nieuwe gracht | 20rd |
20 | Nieuwe gracht 20rd 2 | Nieuwe gracht | 20rd 2 |
21 | Nieuwe gracht 20zw /2 | Nieuwe gracht | 20zw /2 |
22 | Nieuwe gracht 20zw/3 | Nieuwe gracht | 20zw/3 |
23 | Nieuwe gracht 20 zw/4 | Nieuwe gracht | 20 zw/4 |
24 | Bahnhofstr. 4 | Bahnhofstr. | 4 |
25 | Wertstr. 10 | Wertstr. | 10 |
26 | Lindenhof 1 | Lindenhof | 1 |
27 | Nordesch 20 | Nordesch | 20 |
28 | Weilstr. 6 | Weilstr. | 6 |
29 | Harthauer Weg 2 | Harthauer Weg | 2 |
30 | Mainaustr. 49 | Mainaustr. | 49 |
31 | August-Horch-Str. 3 | August-Horch-Str. | 3 |
32 | Marktplatz 31 | Marktplatz | 31 |
33 | Schmidener Weg 3 | Schmidener Weg | 3 |
34 | Karl-Weysser-Str. 6 | Karl-Weysser-Str. | 6 |
Tcl
package require Tcl 8.6
# This is identical to the address task. Skip forward to the next section...
proc split_DE_NL_address {streetAddress} {
set RE {(?x)
^ (.*?) (
(?:\s \d+ [-/] \d+)
|
(?:\s (?!1940|1945)\d+ [a-zI. /]* \d*)
)? $
}
regexp $RE $streetAddress -> str num
return [list [string trim $str] [string trim $num]]
}
set data {
Plataanstraat 5
Straat 12
Straat 12 II
Dr. J. Straat 12
Dr. J. Straat 12 a
Dr. J. Straat 12-14
Laan 1940 – 1945 37
Plein 1940 2
1213-laan 11
16 april 1944 Pad 1
1e Kruisweg 36
Laan 1940-’45 66
Laan ’40-’45
Langeloërduinen 3 46
Marienwaerdt 2e Dreef 2
Provincialeweg N205 1
Rivium 2e Straat 59.
Nieuwe gracht 20rd
Nieuwe gracht 20rd 2
Nieuwe gracht 20zw /2
Nieuwe gracht 20zw/3
Nieuwe gracht 20 zw/4
Bahnhofstr. 4
Wertstr. 10
Lindenhof 1
Nordesch 20
Weilstr. 6
Harthauer Weg 2
Mainaustr. 49
August-Horch-Str. 3
Marktplatz 31
Schmidener Weg 3
Karl-Weysser-Str. 6
}
# Construct the HTML to show
set html "<html><head><meta charset=\"UTF-8\">
<title>split_DE_NL_address</title></head><body>
<table border><tr><th>Address</th><th>Street</th><th>Number</th></tr>"
foreach streetAddress [split $data "\n"] {
set streetAddress [string trim $streetAddress]
if {$streetAddress eq ""} continue
lassign [split_DE_NL_address $streetAddress] str num
append html "<tr><td>$streetAddress</td><td>$str</td><td>$num</td></tr>"
}
append html "</table>
</body></html>"
# Pick a unique filename with .html extension (important!)
set f [file tempfile filename street.html]
fconfigure $f -encoding utf-8
puts $f $html
close $f
##### THE WEB BROWSER LAUNCH CODE MAGIC #####
# Relies on the default registration of browsers for .html files
# This is all very platform specific; Android requires another incantation again
if {$tcl_platform(platform) eq "windows"} {
exec {*}[auto_execok start] "" [file nativename $filename]
} elseif {$tcl_platform(os) eq "Darwin"} {
exec open $filename
} else {
exec xdg_open $filename
}
- Output:
Address | Street | Number |
---|---|---|
Plataanstraat 5 | Plataanstraat | 5 |
Straat 12 | Straat | 12 |
Straat 12 II | Straat | 12 II |
Dr. J. Straat 12 | Dr. J. Straat | 12 |
Dr. J. Straat 12 a | Dr. J. Straat | 12 a |
Dr. J. Straat 12-14 | Dr. J. Straat | 12-14 |
Laan 1940 – 1945 37 | Laan 1940 – 1945 | 37 |
Plein 1940 2 | Plein 1940 | 2 |
1213-laan 11 | 1213-laan | 11 |
16 april 1944 Pad 1 | 16 april 1944 Pad | 1 |
1e Kruisweg 36 | 1e Kruisweg | 36 |
Laan 1940-’45 66 | Laan 1940-’45 | 66 |
Laan ’40-’45 | Laan ’40-’45 | |
Langeloërduinen 3 46 | Langeloërduinen | 3 46 |
Marienwaerdt 2e Dreef 2 | Marienwaerdt 2e Dreef | 2 |
Provincialeweg N205 1 | Provincialeweg N205 | 1 |
Rivium 2e Straat 59. | Rivium 2e Straat | 59. |
Nieuwe gracht 20rd | Nieuwe gracht | 20rd |
Nieuwe gracht 20rd 2 | Nieuwe gracht | 20rd 2 |
Nieuwe gracht 20zw /2 | Nieuwe gracht | 20zw /2 |
Nieuwe gracht 20zw/3 | Nieuwe gracht | 20zw/3 |
Nieuwe gracht 20 zw/4 | Nieuwe gracht | 20 zw/4 |
Bahnhofstr. 4 | Bahnhofstr. | 4 |
Wertstr. 10 | Wertstr. | 10 |
Lindenhof 1 | Lindenhof | 1 |
Nordesch 20 | Nordesch | 20 |
Weilstr. 6 | Weilstr. | 6 |
Harthauer Weg 2 | Harthauer Weg | 2 |
Mainaustr. 49 | Mainaustr. | 49 |
August-Horch-Str. 3 | August-Horch-Str. | 3 |
Marktplatz 31 | Marktplatz | 31 |
Schmidener Weg 3 | Schmidener Weg | 3 |
Karl-Weysser-Str. 6 | Karl-Weysser-Str. | 6 |
Wren
This is based in part on the Nim example and has the same color scheme.
Uses the 'os', 'io' and 'timer' sub-modules of Wren-std.
import "io" for File
import "os" for Process
import "timer" for Timer
import "./pattern" for Pattern
import "./fmt" for Fmt
import "./iterate" for Indexed
var digits = "0123456789"
var p = Pattern.new("+1/s")
var separateHouseNumber = Fn.new { |address|
var len = address.count
var splits = p.splitAll(address)
var size = splits.count
var last = splits[-1]
var penult = splits[-2]
var house
if (digits.contains(last[0])) {
if (size > 2 && digits.contains(penult[0]) && !penult.startsWith("194")) {
house = penult + " " + last
} else {
house = last
}
} else if (size > 2) {
house = penult + " " + last
} else {
house = ""
}
var street = address.take(len - house.count).join().trimEnd()
return [street, house]
}
var addresses = [
"Plataanstraat 5",
"Straat 12",
"Straat 12 II",
"Dr. J. Straat 12",
"Dr. J. Straat 12 a",
"Dr. J. Straat 12-14",
"Laan 1940 - 1945 37",
"Plein 1940 2",
"1213-laan 11",
"16 april 1944 Pad 1",
"1e Kruisweg 36",
"Laan 1940-'45 66",
"Laan '40-'45",
"Langeloërduinen 3 46",
"Marienwaerdt 2e Dreef 2",
"Provincialeweg N205 1",
"Rivium 2e Straat 59.",
"Nieuwe gracht 20rd",
"Nieuwe gracht 20rd 2",
"Nieuwe gracht 20zw /2",
"Nieuwe gracht 20zw/3",
"Nieuwe gracht 20 zw/4",
"Bahnhofstr. 4",
"Wertstr. 10",
"Lindenhof 1",
"Nordesch 20",
"Weilstr. 6",
"Harthauer Weg 2",
"Mainaustr. 49",
"August-Horch-Str. 3",
"Marktplatz 31",
"Schmidener Weg 3",
"Karl-Weysser-Str. 6"
]
var htmlHeader = """
<html>
<head>
<title>Rosetta Code - Start a Web Browser</title>
<meta charset="UTF-8">
</head>
<body bgcolor="#e6e6ff">
<p align="center">
<font face="Arial, sans-serif" size="5">Split the house number from the street name</font>
</p>
<p align="center">
<table border="2"> <tr bgcolor="#9bbb59">
<th>Address</th><th>Street</th><th>House Number</th>
"""
var htmlFooter = """
</table>
</p>
</body>
</html>
"""
File.create("test.html") { |f|
f.writeBytes(htmlHeader + "\n")
for (se in Indexed.new(addresses)) {
var color = (se.index & 1 == 0) ? "#ebf1de" : "#d8e4bc"
var address = se.value
var res = separateHouseNumber.call(address)
var street = res[0]
var house = res[1]
if (house == "") house = "(none)"
f.writeBytes("<tr bgcolor=%(color)><td>%(address)</td><td>%(street)</td><td>%(house)</td></tr>\n")
}
f.writeBytes(htmlFooter + "\n")
}
Process.exec("firefox test.html") // or whatever browser you use
File.flushAll() // flush all streams after user has closed the browser
Timer.wait(2000) // wait 2 seconds
File.delete("test.html") // remove the file