Hello friends
How can I collect and separate the data in the listbox and store it in memory?
listbox content :
15 luk duvar : 1.17 m²
15 luk duvar : 4.81 m²
15 luk duvar : 7.67 m²
15 luk duvar : 7.8 m²
15 luk duvar : 10.4 m²
15 luk duvar : 3.51 m²
15 luk duvar : 4.42 m²
15 luk duvar : 15.6 m²
20 luk duvar : 15.34 m²
20 luk duvar : 15.34 m²
20 luk duvar : 30.94 m²
20 luk duvar : 30.94 m²
desired result: ( Ctrl + C ... clipboard)
Toplam 15 luk duvar toplam = 55.38 m²
Toplam 20 luk duvar toplam = 92.56 m²
string onbes = "15 luk Duvar : ";
string yirmi = "20 luk Duvar : ";
Regex.IsMatch(onbes, @"^\d+$");
foreach (var record in data)
{
var rgx = new Regex(@"(\d+):(\d+)");
var matches = rgx.Matches((string)record);
if (matches.Count < 1) continue;
onbes = double.Parse(matches[0].Groups[1].Value);
yirmi = double.Parse(matches[0].Groups[2].Value);
sum1 += (onbes * 1);
}
Clipboard.SetText("15 luk duvar toplam : " + onbes);
Clipboard.SetText("20 luk duvar toplam : " + yirmi);
* maybe there is another size wall... (Toplam 10 luk Duvar ) 😞
var rgx = new Regex(@": (\d+)");
How should this distinction in regex be?
I have to sum the dotted or undotted values before m².
As said by @norman.yuan in an other topic (which seems related to this one), this is not a good practice to deal with numerical values contained in strings.
Anyway, if, one more time, you want somebody else to write the code for you, this snippet seemed to work in the C# Interactive window.
> var list = new[] {
. "15 luk duvar : 1.17 m²",
. "15 luk duvar : 4.81 m²",
. "15 luk duvar : 7.67 m²",
. "15 luk duvar : 7.8 m²",
. "15 luk duvar : 10.4 m²",
. "15 luk duvar : 3.51 m²",
. "15 luk duvar : 4.42 m²",
. "15 luk duvar : 15.6 m²",
. "20 luk duvar : 15.34 m²",
. "20 luk duvar : 15.34 m²",
. "20 luk duvar : 30.94 m²",
. "20 luk duvar : 30.94 m" };
> var rgx = new Regex(@"^(\d+) luk duvar : (-?\d+([.]\d+)?) m²$");
> list.Select(s => rgx.Match(s).Groups)
. .Where(g => 2 < g.Count)
. .GroupBy(g => g[1].Value)
. .Select(g => $"Toplam {g.Key} luk duvar toplam = {g.Sum(x => double.Parse(x[2].Value))} m²")
. .ToArray()
string[2] { "Toplam 15 luk duvar toplam = 55.38 m²", "Toplam 20 luk duvar toplam = 61.62 m²" }
I wrote this code. There is no problem for now.
I answered because you answered. This was asked a long time ago.
Also, it is not nice to say that someone else writes for you. Then wouldn't my efforts be in vain? 😉
Hello @_gile
I am giving the screen output of the result that I have coded.
* But in the code you sent, how will the wall totals of size 15 and 20 be separated? i didn't see him.
* Data is taken to Listbox, there is no problem here. so the list in your code did not come from listbox1.
How do we edit the code accordingly?
var list = new[] { listBox1.Items };
var rgx = new Regex(@"^(\d+) luk duvar : (-?\d+([.]\d+)?) m²$");
list.Select(s => rgx.Match(s).Groups)
.Where(g => 2 < g.Count)
.GroupBy(g => g[1].Value)
.Select(g => $"Toplam {g.Key} luk duvar toplam = {g.Sum(x => double.Parse(x[2].Value))} m²")
.ToArray();
The code I provided uses Linq with the IEnumerable<T> extension methods. You should study these methods, specifically Select and GroupBy.
You can convert the ObjectCollection of listBox.Items into and IEnumerable<string> with the Cast extension method.
listBox.Items
.Cast<string>()
.Select(s => rgx.Match(s).Groups)
.Where(g => 2 < g.Count)
.GroupBy(g => g[1].Value)
.Select(g => $"Toplam {g.Key} luk duvar toplam = {g.Sum(x => double.Parse(x[2].Value))} m²")
.ToArray();
Anyway, since you don't seem to be comfortable with Linq, you can do the same thing in an imperative way.
var results = new Dictionary<string, double>();
foreach (string s in listBox.Items)
{
var groups = rgx.Match(s).Groups;
if (2 < groups.Count)
{
if (results.ContainsKey(groups[1].Value))
results[groups[1].Value] += double.Parse(groups[2].Value);
else
results[groups[1].Value] = double.Parse(groups[2].Value);
}
}
var resultStrings = new List<string>();
foreach (KeyValuePair<string, double> pair in results)
{
resultStrings.Add($"Toplam {pair.Key} luk duvar toplam = {pair.Value} m²");
}
listBox.Items
.Cast<string>()
.Select(s => rgx.Match(s).Groups)
.Where(g => 2 < g.Count)
.GroupBy(g => g[1].Value)
.Select(g => $"Toplam {g.Key} luk duvar toplam = {g.Sum(x => double.Parse(x[2].Value))} m²")
.ToArray();
Now how can I get this code result in the message box?
I typed in the message box and called g.
It's okay if you give it as an alternative. but i don't understand this code...
Can't find what you're looking for? Ask the community or share your knowledge.