ISourceCode

Make the frequent cases fast and the rare case correct

LINQ way to populate a 2-D array from a text file

Found this cool method to populate a 2-D array while reading a file which contains a 2-D array split by comma – courtesy this post in stackoverflow.

for example
A B C
D E F G
H I J

Note the array values of array[0][3] and array[2][3] are followed by a spaces. Your text file needs to have a space after C and J so that the below one liner statement can read it as a 4×4 matrix. I showed this for odd case scenario of a matrix in a text file

string[][] array = File.ReadAllLines("file path").Select(s => s.Split(',')).ToArray();
About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: