Folge dem Video um zu sehen, wie unsere Website als Web-App auf dem Startbildschirm installiert werden kann.
Anmerkung: Diese Funktion ist in einigen Browsern möglicherweise nicht verfügbar.
Was ist eigentlich wenn eine Value mal keine nummer ist sondern ein String, kann man das noch schnell ändern das das auch geht oder wäre es nötig alles umzuschreiben?
int[] vals = new int[values.Count];
int t;
for (int i = 0; i < vals.Length; i++)
{
if (int.TryParse(values[i], out t))
vals[i] = t;
else
throw new Exception("Token not a number");
}
string[] vals = values.ToArray();
(values (values1 2) (values2 0) (values3 6) (values4 10 20 30 40))
(values (values1 22) (values2 (550 40 111) (1 2 3)))
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
class Program
{
static void Main(string[] args)
{
string text =
"(values (value1 40) (value2 0) (value3 6) (value4 80) \r\n" +
"(value5 70) (value6 10 20))\r\n" +
"(values (value1 70) (value2 0) (value3 6) (value4 80) \r\n" +
"(value5 70) (value6 10 20 30 40))\r\n" +
"(values (value1 80) (value2 0) (value3 6) (value4 80) \r\n" +
"(value5 70) (value6 10 20 30)) ; testkommentar.. :)\r\n" +
"(values (value1 90) (value2 0) (value3 6) (value4 40) \r\n" +
"(value5 70) (value6 10 20 30 20))\r\n" +
"(values (value1 10) (value2 0) (value3 6) (value4 80) ; noch ein kommentar... ;)\r\n" +
"(value5 70) (value6 10 20 30 50))\r\n" +
"(values (values1 22) (values2 (550 40 111) (1 2 3)))\r\n";
var result = Parse(text);
for (int i = 0; i < result.Count; i++)
{
Console.WriteLine("\r\n -- Zeile {0}", i);
foreach (string key in result[i].Keys)
{
Console.WriteLine("Name: {0}", key);
for (int j = 0; j < result[i][key].Length; j++)
Console.Write("{0}, ", result[i][key][j]);
Console.WriteLine();
}
}
Console.ReadLine();
}
private static List<Dictionary<string, string[]>> Parse(string text)
{
var data = new List<Dictionary<string, string[]>>();
bool outer = false;
bool inner = false;
bool tuple = false;
bool first = false;
string key = null;
string tupledata = null;
List<string> values = null;
Dictionary<string, string[]> currentData = new Dictionary<string, string[]>();
foreach (string token in Tokenize(text))
{
switch (token)
{
case "(":
if (inner)
{
tuple = true;
tupledata = "";
}
else if (outer)
{
inner = true;
first = true;
values = new List<string>();
}
else
outer = true;
break;
case ")":
if (tuple)
{
tuple = false;
values.Add(tupledata);
tupledata = null;
break;
}
if (inner)
{
inner = false;
string[] vals = values.ToArray();
currentData.Add(key, vals);
}
else if (outer)
{
outer = false;
if (currentData != null)
data.Add(currentData);
currentData = new Dictionary<string, string[]>();
}
else
throw new Exception("Unexpected token ')'");
break;
default:
if (token[0] == ';') // kommentar.. ;)
break;
if (tuple)
{
tupledata += token + " ";
break;
}
if (!inner)
break;
if (first)
{
key = token;
first = false;
}
else
{
values.Add(token);
}
break;
}
}
return data;
}
private static IEnumerable<string> Tokenize(string text)
{
string token = "";
bool isCommentToken = false;
foreach (char c in text)
{
switch (c)
{
case '\n':
case '\r':
if (isCommentToken)
{
isCommentToken = false;
yield return token;
token = "";
}
goto case ' ';
case ' ':
if (isCommentToken)
{
token += c;
break;
}
if (token.Length > 0)
yield return token;
token = "";
break;
case '(':
case ')':
if (isCommentToken)
{
token += c;
break;
}
if (token.Length > 0)
yield return token;
yield return c.ToString();
token = "";
break;
case ';':
if (isCommentToken)
{
token += c;
break;
}
if (token.Length > 0)
yield return token;
isCommentToken = true;
token = c.ToString();
break;
default:
token += c;
break;
}
}
if (token.Length > 0)
yield return token;
}
}