C
C#2mo ago
The Father

✅ confusing list logic

So, I have a program that allows me delete objects in an array through the console, and the objects are all displayed to the console like in this image (AT THE BOTTOM) And for the objects that I want to delete, I need to the 1-based indices of the objects I want to delete, or a range of indices that i want to delete But I'm having a problem with the code: each iteration deletes objects before the next iteration/deletion, interrupting the next iteration/deletion
public static void PromptDeletion<T>(ref T[] values, Func<T, string> repr)
{
List<T> tempValues = new(values);

ShowValuesInOrder([.. tempValues], repr);

Console.WriteLine("Values to delete> ");
string input = Console.ReadLine() ?? "";

string[] deletions = input.Split(',');
// Tries to delete the objects at the specified indices, but each iteration removes objects from the array BEFORE the NEXT iteration/deletion
try
{
foreach (string value in deletions)
{
if (value.Count(s => s == '-') == 1)
{
int[] ends = value.Split('-').Select(n => Convert.ToInt32(n) - 1).ToArray();
tempValues.RemoveRange(ends[0], ends[1] - ends[0]);
}
else
{
tempValues.RemoveAt(Convert.ToInt32(value));
}
}

values = [.. tempValues];

}
catch (Exception)
{
ConsoleExt.WriteError("There was a problem removing the values.");
}
}
public static void PromptDeletion<T>(ref T[] values, Func<T, string> repr)
{
List<T> tempValues = new(values);

ShowValuesInOrder([.. tempValues], repr);

Console.WriteLine("Values to delete> ");
string input = Console.ReadLine() ?? "";

string[] deletions = input.Split(',');
// Tries to delete the objects at the specified indices, but each iteration removes objects from the array BEFORE the NEXT iteration/deletion
try
{
foreach (string value in deletions)
{
if (value.Count(s => s == '-') == 1)
{
int[] ends = value.Split('-').Select(n => Convert.ToInt32(n) - 1).ToArray();
tempValues.RemoveRange(ends[0], ends[1] - ends[0]);
}
else
{
tempValues.RemoveAt(Convert.ToInt32(value));
}
}

values = [.. tempValues];

}
catch (Exception)
{
ConsoleExt.WriteError("There was a problem removing the values.");
}
}
I'm looking for a way to remove all of the specified objects from the array all at once, or something else that achieves the same result
No description
5 Replies
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
The Father
The Father2mo ago
which is what i'm trying to fix how do you i suggest i remove them without moving the objects around for different iterations
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
The Father
The Father2mo ago
mm i can try sorting everything by descending numerical value first and then delete it in that order i'll try that thanks
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server