Adding and Couting items in a list - c#

Let's say I have a file that looks like this:
R34 128590 -74.498 109.728 0 0805_7
R33 128590 -74.498 112.014 0 0805_7
R15 128588 -68.910 127.254 0 0805_7
R32 128587 -65.354 115.189 0 0805_7
R35 128587 -65.354 117.348 0 0805_7
R38 128590 -65.354 119.507 0 0805_7
What I want to do is add the 2nd column to a list and have a counter count how many times that item occurs and outputs it with the number and then the counted amount of that number.
Is there a way to do this using a List? If so, how could I go about doing that?
I have tried messing around with things and this is where I was heading.. but it does not work properly
int lineCount = 1;
int itemCounter = 0;
foreach (var item in aListBox.Items)
{
// Creates a string of the items in the ListBox.
var newItems = item.ToString();
// Replaces any multiple spaces (tabs) with a single space.
newItems = Regex.Replace(newItems, #"\s+", " ");
// Splits each line by spaces.
var eachItem = newItems.Split(' ');
###
### HERE is where I need help ###
###
List<string> partList = new List<string>();
partList.Add(eachItem[1]);
if (partList.Contains(eachItem[1]))
itemCounter++;
else
partList.Add(eachItem[1]);
sw.WriteLine(lineCount + ": "+ partList + ": " + itemCounter);
lineCount++;
}
SO for the example above, it would output this:
1: 128590: 3 #lineCount, partList, itemCounter
2: 128588: 1
3: 128587: 2
Can someone help me figuring out how to properly do this?

use linq with count and group by (see Count- Grouped section).
create your partList outside the foreach loop and add each item to it inside the loop , so that it would contain all of the elements:
List<string> partList = new List<string>();
foreach (var item in aListBox.Items)
{
//regex stuff here...
partList.Add(eachItem[1]);
}
(in your example- {128590, 128590, 128588, 128587, 128587, 128590})
and then use LINQ to output the result-
var elementsWithCounts = from p in partList
group p by p into g
select new { Item = g.Key, Count = g.Count()};

I would either use a Linq query or a Dictionary
something like
List<string> items = new List<string>{"128590", "128590", "128588", "128587", "128587", "128590"};
Dictionary<string,int> result = new Dictionary<string,int>();
foreach( int item in items )
{
if(result.ContainsKey(item) )
result[item]++;
else
result.Add(item,1);
}
foreach( var item in result )
Console.Out.WriteLine( item.Key + ":" + item.Value );

Once you have the items split by space, I'm assuming you have a string array looking like so:
[0] = "R34"
[1] = "128590"
[2] = "-74.498"
[3] = "109.728"
[4] = "0"
[5] = "0805_7"
You can simply perform this operation with a Group By operation.
var items = aListBox.Items.Select(x => /* Split Code Here and Take Element 1 */).GroupBy(x => x);
foreach(var set in items)
{
Console.WriteLine(set.Key + " appeared " + set.Count() + " times.");
}

Basically, you are trying to do this by iterating once, and that is not really going to work, you are going to have to iterate twice, otherwise you will wind up doing an output every time you loop in the foreach, and even if your accurate you are going to be outputting a new line each time. If you need to really use a List instead of a keyed dictionary or hashtable which would be idea for this (key = number, value = count), then you need to build the list first, then summarize the list. You can either use LINQ Group By (which is a bit terse), or create a function that does something similar to what you already have. If you are trying to learn concepts, look at the code below, it could be more condensed but this should be fairly easy to read.
List<string> partList = new List<string>();
List<string> displayedNumbers = new List<int>();
// Build the original list first.
foreach (var item in aListBox.Items)
{
// Creates a string of the items in the ListBox.
var newItems = item.ToString();
// Replaces any multiple spaces (tabs) with a single space.
newItems = Regex.Replace(newItems, #"\s+", " ");
// Splits each line by spaces.
var eachItem = newItems.Split(' ');
partList.Add(eachItem[1]);
}
// Now run through that list and count how many times the same number occurs.
// You will need two loops for this since your list is a single dimension collection.
foreach(var number in partList)
{
var innerList = partList;
// set this to zero because we are going to find at least 1 duplicate.
var count = 0;
foreach(var additionalNumber in innerList)
{
if(additionalNumber == number)
{
// If we find anymore increase the count each time.
count += 1;
}
}
// Now we have the full count of duplicates of the outer number in the list.
// If it has NOT been displayed, display it.
if(!displayedNumbers.Contains(number))
{
sw.WriteLine(partList + ": " + count);
displayedNumbers.Add(number);
}
}

Use a hash table instead ofa list. You can save the key as 128590,... and the value the number of times it has occurred.
Before you insert the new value check if it is already present in the hashtable by using the Contains operation and if it is increment the value.

I think the biggest problem is getting from raw lines of your text field to individual values. My guess is this is a tab-delimited file with a known constant number of columns, in which case you could use String.Split() to separate the sub-strings. Once you have the strings separated, you can count the instances of the proper column pretty easily with a little LINQ. Given a list or collection of your file's lines:
var histogram = myListOfLines
//Split each string along spaces or tabs, and discard any zero-length strings
//caused by multiple adjacent delimiters.
.Select(s=>s.Split(new[]{'\t',' '}, StringSplitOptions.RemoveEmptyEntries))
//Optional; turn the array of strings produced by Split() into an anonymous type
.Select(a=>new{Col1=a[0], Col2=a[1], Col3=a[2], Col4=a[3], Col5=a[4]})
//Group based on the values of the second column.
.GroupBy(x=>x.Col2)
//Then, out of the grouped collection, get the count for each unique value of Col2.
.Select(gx=>new{gx.Key, gx.Count()});

Related

How to search through combobox with a string containing a wildcat?

I have a combo-box that contains lots of entries like this small extract
1R09ST75057
1R11ST75070
1R15ST75086
1R23ST75090
2R05HS75063
2R05ST75063
3R05ST75086
2R07HS75086
The user now enters some information in the form that result in a string being produced that has a wildcat (unknown) character in it at the second character position
3?05ST75086
I now want to take this string and search\filter through the combo-box list and be left with this item as selected or a small set of strings.
If I know the string without the wildcat I can use the following to select it in the Combo-box.
cmbobx_axrs75.SelectedIndex = cmbobx_axrs75.Items.IndexOf("2R05HS75063");
I thought I could first create a small subset that all have the first char the same then make a substring of each minus the first two chars and check this but I can have a large amount of entries and this will take too much time there must be an easier way?
Any ideas how I can do this with the wildcat in the string please?
Added info:
I want to end up with the selected item in the Combobox matching my string.
I choose from items on the form and result in string 3?05ST75086. I now want to take this and search to find which one it is and select it. So from list below
1R05ST75086
2R05ST75086
3R05ST75086
6R05ST75086
3R05GT75086
3R05ST75186
I would end up with selected item in Combo-box as
3R05ST75086
You could use regular expressions. Something like this:
string[] data = new string[]
{
"1R09ST75057",
"1R11ST75070",
"1R15ST75086",
"1R23ST75090",
"2R05HS75063",
"2R05ST75063",
"3R05ST75086",
"2R07HS75086"
};
string pattern = "3*05ST75086";
string[] results = data
.Where(x => System.Text.RegularExpressions.Regex.IsMatch(x, pattern))
.ToArray();
You can use a regular expression for this task. First, you need a method to convert your pattern string to Regex like this (it should handle "*" and "?" wildcards):
private static string ConvertWildCardToRegex(string value)
{
return "^" + Regex.Escape(value).Replace("\\?", ".").Replace("\\*", ".*") + "$";
}
Then you will use it like the following:
List<string> comboBoxValues = new List<string>()
{
"1R09ST75057",
"1R11ST75070",
"1R15ST75086",
"1R23ST75090",
"2R05HS75063",
"2R05ST75063",
"3R05ST75086",
"2R07HS75086"
};
string searchPattern = "3?05ST75086";
string patternAsRegex = ConvertWildCardToRegex(searchPattern);
var selected = comboBoxValues.FirstOrDefault(c => Regex.IsMatch(c, patternAsRegex));
if (selected != null)
{
int selectedIndex = comboBoxValues.IndexOf(selected);
}
This assumes you only care about first found match. If you need all matches then substitute FirstOrDefault(...) with Where(...) clause and swap "if" statement with a foreach loop.
Thanks to all that helped I used a combination of items from all answers so everyone helped me answer this.
I added this function from the answers as it seems a good idea, thanks
private static string ConvertWildCardToRegex(string value)
{
return "^" + Regex.Escape(value).Replace("\\?", ".").Replace("\\*", ".*") + "$";
}
Then I get the combo box items into a list. I search the list and make some more decisions based on the result of the search.
List<string> comboBoxValues = new List<string>();
for (int i = 0; i < cmbobx_in_focus.Items.Count; i++)
{
comboBoxValues.Add(cmbobx_in_focus.GetItemText(cmbobx_in_focus.Items[i]));
}
string[] results = comboBoxValues
.Where(x => Regex.IsMatch(x, ConvertWildCardToRegex(lbl_raster_used.Text)))
.ToArray();
I now have array called results which is easy to work with.

Remove rows where column contains specific text

I want to remove all the rows of the data whose columns contains ? e.g. in around 100 rows for Column Status I am getting value as Unknown?, Error?, InProgress, Done
So , I want to remove all the rows which contains ?
Below are the code I am using
//I am splitting the string on the basis of delimeter ,
var data = from val in UserData
select val.Split(',');
//Below code is not working
var filterdata = from rows in data
where rows.Contains("?")
select rows;
You forgot to invert the contains:
string[] someStringArray = new string[]
{
"\"ABC\" ,\"Error?\",\"OK\"",
"\"DEF\",\"Inprogress\",\"FINE\"",
"1,2,3",
"?,2,3",
"1,?,3",
"4,5,6"
};
//I am splitting the string on the basis of delimeter ,
var data = from val in someStringArray
select val.Split(',');
//Below code is not working
var filterdata = from rows in data
where !rows.Contains("?") // "!" to select the rows WITHOUT "?"
select rows;
foreach (var item in filterdata)
{
foreach (var i in item)
{
Console.Write(i + ",");
}
Console.WriteLine();
}
return;
Result:
"DEF","Inprogress","FINE",
4,5,6,
This code is perfectly working, I think.
Beside this, I doing a wild guess: You're not searching for quesionmarks "?". The "?" is a character which is often shown if the character can't be shown in your expected encoding.
Have a look which number your chars have:
var chars = someStringArray.SelectMany(s => s.Select(c => c));
foreach (var item in chars.GroupBy(g => g.ToString() + " (" + ((int)g) + ")"))
{
Console.WriteLine(item.Key + ": " + item.Count());
}
Real questionmarks have a 63. If not you've got encoding problems..
You wrote:
I want to remove all the rows of the data whose columns contains "?"
You can never change the input sequence using LINQ functions. So you can't remove rows from your original data using LINQ.
What you can do, is use your data to create a new sequence that doesn't contain question marks. If desired, you can replace your original data with the new sequence.
Looking at your code, it seems that UserData is a sequence of strings, of which you expect that these strings contains comma separated values.
You want to split these CSV strings into their columns, but you don't want rows where any of your columns contain "?"
"A,?,B,C" => do not use this one, one of the column values equals "?"
"A,B,C" => use this one, none of the column values equal "?"
"A, Hello?, B" => use this one, although the second column contains a question mark
this second column is not equal to question mark
This is done as follows:
static readonly char[] separatorChars = new char[] {','}
const string questionMark = "?";
static readonly IEqualityComparer<string> comparer =
var rowsWithoutQuestionMarkValues = userData
// Split each line into column values, using comma as separator
.Select(line => line.Split(separatorChar)
// do not use the line if any of the columns equals the question mark
.Where(splitLine => !splitLine.Any(column => column == questionMark));
If your code might be running in a culture where a questionmark might look differently, for instance: "分号", consider using an IEqualityComparer<string>
readonly IEqualityComparer<string> comparer = GetStringComparerForMyCulture();
var result = ...
.Where(splitLine => !splitLine.Any(column => comparer.Equals(column, questionMark));

Populate ArrayList 2 columns and keep a count in second column of each occurrence c#

I'm new to ASP.NET C#. Trying to create an ArrayList with 2 columns one for the value (string) and one for counting how many of each. While adding values I need to search the ArrayList to find if the value already exist, if so add 1, if not, add it to the array and set count column to 1. Can someone provide a bit of code sample? If there is a better approach then I'd like to hear it.
private static Dictionary<string, int> values = new Dictionary<string, int>();
private static void Add(string newValue)
{
if(values.ContainsKey(newValue))
{
values[newValue]++; // Increment count of existing item
}
else
{
values.Add(newValue, 1); // Add new item with count 1
}
}
If you're just starting with a list of strings, there are plenty of simpler ways to do this.
I'd probably use the GroupBy extension here
List<string> items = GetItems(); // from somewhere
var groups = items.GroupBy(i => i);
var countedItems = groups.Select(g => new
{ Value = g.First(), HowMany = g.Count() });
Then putting into an ArrayList, if you want:
var arrayList = new ArrayList();
foreach (var thing in countedItems)
{
arrayList.Add(thing.Value + " " thing.HowMany);
}
But I'd probably prefer to put this into a Dictionary, because you know that each word will map to just one value - the number of times it appears.
var result = countedItems.ToDictionary(i => i.Value, i => i.HowMany);

Returning list of list values (consolidated)

I want to be able to return a list of all "list values" coming from the query.. 'query' below returns multiple rows of results back from db, each as an item in a list. A sample result back from db would look like...
sample query results when I put break point: (this is what first line of code below 'query' returns from db)
Name = John ; Address = 1230, Ewded ; listOfCities = "NY, CH, LA"
Name = Eric; Address = 12 , Ewded ; listOfCities = "BO, SE, OR"
Code:
List<Index.Result> query = getresultsbackfromdb();
// query content at this point looks like above 1,2
List<string> result = new List<string>();
foreach (var item in query)
{
results.Add(item.listCities);
//'results' list takes in string and not a list
//How do I return a consolidated list of items
}
return result; // this should have ""NY, CH, LA, BO, SE, OR"
//I am trying to get a list of all cities from 1,2 included in
//one single list.
There is method in a List that allows you to add multiple items
foreach (var item in query)
{
results.AddRange(item.listCities);
}
Docs for List.AddRange Method.
Also, just in case if you need to filter out some repeated items, you can use a Distinct LINQ method.
You can try this code based on Split Method
var result = yourString.Split(',');
var input = "NY, CH, LA";
var result = input.Split(',');
And you can save this value in List<object>
var list = new List<object>();
list.Add(result );
You want the AddRange and string.Split methods
results.AddRange(string.Split(',', item.ListCities));
string.Split will split the string into an array wherever it finds the given character, and add range will add all items in an array to the list.
Try this:
var result = query.SelectMany(x => x.listOfCities.Split(','));
Or use
var result = query.SelectMany(x => x.listOfCities.Split(',')).Distinct();
to get the list without duplicates.
If you like Linq then you could do this one line:
using System.Linq;
List<string> result = query.SelectMany(s => s.listCities).ToList();
(This does essentially the same thing as oleksii's AddRange.)

c# Arrays - Index Out of Range Exception

I am getting indexoutofrangeexception (see ----> pointer for the line generating the error down below in the code). The program loops through the header and line item records in a dataset tables. The tables have a relationship. My sample data has 2 headers, each with 2 lines. The progam has two loops, the first one loops through the header records and the second one loops through the child records of the header.
Part of the program:
// ***** PO Header and Line
int ln;
ln = 0;
// Create an eConnect PO Header node object
taGLTransactionHeaderInsert jeh = new taGLTransactionHeaderInsert();
// Create an array for lineitems
taGLTransactionLineInsert_ItemsTaGLTransactionLineInsert[] lineitems = new taGLTransactionLineInsert_ItemsTaGLTransactionLineInsert[ln];
foreach (DataRow dtrHDR in ds.Tables["Header"].Rows)
{
Array.Clear(lineitems, 0, ln);
jeh.BACHNUMB = "Sheraz";
jeh.JRNENTRY = jenoint;
jeh.REFRENCE = dtrHDR["Reference"].ToString();
jeh.SOURCDOC = dtrHDR["AvantisJE"].ToString();
jeh.USERID = System.Environment.UserName;
jeh.TRXDATE = System.DateTime.Now.ToString();
ln = 0;
foreach (DataRow dtrLine in dtrHDR.GetChildRows("HdrLine"))
{
// Populate the elements of the taPoLIne_ItemsTaPOLine XML node
taGLTransactionLineInsert_ItemsTaGLTransactionLineInsert jel = new taGLTransactionLineInsert_ItemsTaGLTransactionLineInsert();
jel.BACHNUMB = jeh.BACHNUMB;
jel.JRNENTRY = jeh.JRNENTRY;
jel.ACTNUMST = dtrLine["GreatPlains"].ToString();
jel.DEBITAMT = Convert.ToDecimal(dtrLine["Debit"].ToString());
//Avantis Inv Trx Key
jel.ORDOCNUM = dtrLine["AvantisJE_Line"].ToString();
// Avantis GL Trx Type
jel.ORTRXDESC = dtrLine["transactiontypename"].ToString();
//Add POLine to an Array
lineitems[ln] = jel; ----------------> I get an error here!
ln = ln + 1;
Array.Resize(ref lineitems, ln + 1);
}
}
You are accessing an index that doesn't yet exist.
//Add POLine to an Array
lineitems[ln] = jel; ----------------> I get an error here!
ln = ln + 1;
Array.Resize(ref lineitems, ln + 1);
You need to change the order to :
//Add POLine to an Array
Array.Resize(ref lineitems, ln + 1);
lineitems[ln] = jel; ----------------> should be fixed, no error here!
ln = ln + 1;
EDIT: Now that the immediate problem is out of the way, on to a better implementation.
Arrays are of a fixed sized, resizing an array is an expensive operation (basically it entails creating a copy with a new size). typically you would use these after identifying a performance bottle neck. In most cases it would be much better to use a List.
I'd recommend changing this line:
// Create an array for lineitems
taGLTransactionLineInsert_ItemsTaGLTransactionLineInsert[] lineitems =
new taGLTransactionLineInsert_ItemsTaGLTransactionLineInsert[ln];
to:
var lineitems = new List<taGLTransactionLineInsert_ItemsTaGLTransactionLineInsert>();
and then to add to it you simply do
lineitems.Add(jel);
to iterate over them would be:
for (var ln in lineitems) {
// whatever you want to do with a line.
}
to acccess a specific item by index would be:
lineitems.Item(i); // get the ith item in the list.
This is because you created an array with 0 elements and try to insert an element on position 0. This will not work. You can fix it by declaring the array with a size of 1 to begin with:
// Create an array for lineitems
taGLTransactionLineInsert_ItemsTaGLTransactionLineInsert[] lineitems = new taGLTransactionLineInsert_ItemsTaGLTransactionLineInsert[1];
However, resizing an array on the fly is not the idiomatic .NET way of doing this. You could use a List<T>, which takes care of resizing for you, and leaving you with cleaner code and possibly better performance.
It appears that you're trying to increase the size of the array by setting a value, like you might do in JavaScript. C# arrays are not like that. You need to create them at the size you want them to be when finished.
Alternately, you could use a List object, using the Add() method to put new content into the list.
lineitems is obviously not the same size as the row collection returned by dtrHDR.GetChildRows("HdrLine"). You are creating an array of zero elements and then trying to index into it. If you want it to match the size of dtrHDR.GetChildRows("HdrLine") then you need to call that first and initialize the array after you can get the count.
Instead of using an array why don't you use a List<T> and just push items onto it? No need to worry about IndexOutOfRange exceptions anymore.
You need to initialize the array before putting anything in to it. call the .resize first.
those lines are the problem
int ln;
ln = 0;
// Create an array for lineitems
taGLTransactionLineInsert_ItemsTaGLTransactionLineInsert[] lineitems = new taGLTransactionLineInsert_ItemsTaGLTransactionLineInsert[ln];
You create table of 0 elements.
You have an off-by-one error in your program:
int ln = 0
.
.
.
taGLTransactionLineInsert_ItemsTaGLTransactionLineInsert[] lineitems = new taGLTransactionLineInsert_ItemsTaGLTransactionLineInsert[ln];
.
.
.
lineitems[ln] = jel;
You are initializing an array of 0 elements, then trying to set the first element (element[0]) to a value.

Categories