Need an algorithm (coding required in C#)

Posted by Agopi.net under C# on 3/29/2011 | Points: 10 | Views : 1937 | Status : [Member] | Replies : 9
Hi friends,

I need an algorithm for the below concept. Where ever we find the "D" in the below (bold letter) we need to capture the image id (ARV001) again we need to find the last "C" (see the bold letter) (make sure this is the last "C" before of the next "D") here we need to capture the image id (see the bold).

Then the same concept we need to do upto complete this task.

input is in text file (notepad). Below is the input.

IM,ARV001,D ,0,@arv;ARV\image\00;ARV001.tif;2
IM,ARV002, ,0,@arv;ARV\image\00;ARV002.tif;2
IM,ARV003,C,0,@arv;ARV\image\00;ARV003.tif;2
IM,ARV004,C,0,@arv;ARV\image\00;ARV004.tif;2
IM,ARV005, ,0,@arv;ARV\image\00;ARV005.tif;2
IM,ARV006, ,0,@arv;ARV\image\00;ARV006.tif;2
IM,ARV007,C ,0,@arv;ARV\image\00;ARV007.tif;2
IM,ARV008 , ,0,@arv;ARV\image\00;ARV008.tif;2
IM,ARV009,D,0,@arv;ARV\image\00;ARV009.tif;2
IM,ARV010, ,0,@arv;ARV\image\00;ARV010.tif;2
IM,ARV011, ,0,@arv;ARV\image\00;ARV021.tif;2
IM,ARV012,C,0,@arv;ARV\image\00;ARV012.tif;2

output (need)

ARV001 ARV008
ARV009 ARV012

Could you please help me how to write the algorithm or C# coding.




Responses

Posted by: WilPeters on: 3/29/2011 [Member] Starter | Points: 25

Up
0
Down
I assumed you highlighted the wrong image id (ARV008 instead of ARV007), since in your description it says you need the image id from the line with the last C.

This code should do what you need. It looks for the D (and "D " which i found in the example data)

void PrintFileNames(string filename)
{
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
StreamReader sr = new StreamReader(fs);
List<string> fileData = new List<string>();
string line;
while((line = sr.ReadLine()) != null)
{
fileData.Add(line);
}
sr.Close();
fs.Close();
string lastC = null;
bool first = true;
for (int i = 0; i < fileData.Count; i++)
{
string[] values = fileData[i].Split(new char[] {','});
if(values[2].Contains('D'))
{
if (!first)
{
Console.Write(lastC + "\n");
lastC = null;
}
else
{
first = false;
}
Console.Write(values[1] + "\t");
}
else if(values[2].Contains('C'))
{
lastC = values[1];
}
}
if (lastC != null)
{
Console.Write(lastC + "\n");
}
}

Agopi.net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gsolvers on: 3/31/2011 [Member] Starter | Points: 25

Up
0
Down
Is this sorted out?

Best Regards,

VG
www.TeacherJi.com

Agopi.net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Agopi.net on: 3/31/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Peters,

The above example is correct which i provided. I am sorry if my way of thought is unclear. I need the first image ID that where the "D" is occurred and last image ID of the last "C".

Please see the below which i need the exact image id.

Beginning Ending
ARV001 ARV002
ARV003 ARV003
ARV004 ARV006
ARV007 ARV008
ARV009 ARV011
ARV012 ARV012

Hear i need

Beginning Ending
ARV001 ARV008
ARV009 ARV012

I hope this is clear to you. Please guide me.

Agopi.net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: WilPeters on: 3/31/2011 [Member] Starter | Points: 25

Up
0
Down
so the 'C' doesn't really matter, you just need the value of the last line before the line with the 'D'?

Agopi.net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Agopi.net on: 4/2/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Peters,

Sorry for the Late Reply.

Yes, You are correct. I need the first "D" image ID and then last line Image ID that the before of the next "D".

Thank You.

Agopi.net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Agopi.net on: 4/3/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Peters,

While execute the program the below error was occurred. I think i need to declare the system namespace for split function. Could you please help me.

Error 1 'System.Collections.Generic.List<string>' does not contain a definition for 'split' and no extension method 'split' accepting a first argument of type 'System.Collections.Generic.List<string>' could be found (are you missing a using directive or an assembly reference?) E:\Gopi_Important\ConsoleApplication2\ConsoleApplication2\Program.cs 38 44 ConsoleApplication2


Agopi.net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: WilPeters on: 4/3/2011 [Member] Starter | Points: 25

Up
0
Down
i changed the whole method to get the info you need. Try this

static void PrintFileNames(string filename)

{
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
StreamReader sr = new StreamReader(fs);
List<string> fileData = new List<string>();
string line;
while((line = sr.ReadLine()) != null)
{
fileData.Add(line);
}
sr.Close();
fs.Close();
string lastLineBeforeD = null;
bool first = true;
for (int i = 0; i < fileData.Count; i++)
{
string[] values = fileData[i].Split(new char[] {','});
if(values[2].Contains('D'))
{
if (!first)
{
Console.Write(lastLineBeforeD + "\n");
lastLineBeforeD = null;
}
else
{
first = false;
}
Console.Write(values[1] + "\t");
}
else
{
lastLineBeforeD = values[1];
}
}
if (lastLineBeforeD != null)
{
Console.Write(lastLineBeforeD + "\n");
}
}


Agopi.net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Agopi.net on: 4/4/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Peters,

Thanks a lot.

Your code is working fine. My problem was solved.

Agopi.net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Agopi.net on: 4/6/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Peters,

I had changed the code slightly. Below is the full code.

static void PrintFileNames(string filename)


{

FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
StreamReader sr = new StreamReader(fs);
List<string> fileData = new List<string>();
string line;
while((line = sr.ReadLine()) != null)
{
fileData.Add(line);
}
sr.Close();
fs.Close();
string lastLineBeforeD = null;
bool first = true;
for (int i = 0; i < fileData.Count; i++)
{
string[] values = fileData[i].Split(new char[] { ',' });

if(values[2].Contains('D'))
{
if (!first)
{
Console.Write(lastLineBeforeD + "\n");
lastLineBeforeD = null;
}
else
{
first = false;
}
Console.Write(values[1] + "\t");
}
else
{
lastLineBeforeD = values[1];
}
}
if (lastLineBeforeD != null)
{
Console.Write(lastLineBeforeD + "\n");
}
}


}
}


Thanks for the help.

Agopi.net, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response