[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6910
  • Last Modified:

How to compare arrays in C#

Hi Experts,

I have a C# function that takes two string parameters and return a string.

The problem:
As you might have noticed, I am targetting a specific pattern 12, 12BN, 12xxxBN inside the the previousTasks variable.

What I would like to do is:
- First, check the  previousTasks string and see if it contains that pattern
- Second, check the requestedTasks string and look for the pattern substring

The point is, if both the previousTasks and requestedTasks strings contain the pattern then make sure the previousTasks's one never gets anywhere, but
replace or switch it with the  requestedTasks's one. That's the reason why I decided to use two arrays and compare them, then add them to a string

However it is not quite working well. I was also wondering if there was any better or efficient way to do it.

Please help!

here's a typical variable scenario:
previousTasks==12BN|45|RS34|00TY
requestedTasks == 12UTBN|09


Here's the function:
---------------------------------------------------------------------------------------------------
public static string InsertServiceTask(string previousTasks, string requestedTasks)
{
string[] arPreviousTasks = previousTasks.Split(ServiceTaskDelimiter.ToCharArray());
string[] arRequestedTasks = requestedTasks.Split(ServiceTaskDelimiter.ToCharArray());
string serviceTasks = string.Empty;

      for (int i=0; i<arRequestedTasks.Length; i++)
      {
            for (int j=0; j<arPreviousTasks.Length; j++)
            {
                  /**********************************************************************
                  * Step 1 - Ensure that there is not duplicate of service
                  /**********************************************************************/
                  if(arRequestedTasks[i] != arPreviousTasks[j])
                              {
                  /****************************************************************************
                  * Step 2 - Check/Validate the service task and see if it is credit related
                  /****************************************************************************/
                  //strTask = resetTask(arPreviousTasks[j], arRequestedTasks[i]);
                  if((arPreviousTasks[j] == "12")
                        &&(!arRequestedTasks[i].StartsWith("12")))
                  arPreviousTasks[j] = arRequestedTasks[i];


                  if(arPreviousTasks[j].StartsWith("12")
                        && arRequestedTasks[i].EndsWith("BN"))
                  arPreviousTasks[j] = string.Concat("12", "BN") ;


                  if((arPreviousTasks[j].StartsWith("12"))
                        && ((arRequestedTasks[i].StartsWith("12"))
                        &&(!arRequestedTasks[i].EndsWith("BN"))))
                  arPreviousTasks[j] = 12 ;

                  /**********************************************************************
                  * Step 3 - Add service to the string
                  /**********************************************************************/
                  if (serviceTasks.IndexOf(arPreviousTasks[j]) == -1)
                  {
                        if (serviceTasks.Length == 0)
                              serviceTasks +=  arPreviousTasks[j];
                        else
                              serviceTasks += ServiceTaskDelimiter + arPreviousTasks[j];
                  }
                              }
            }//Go to the next PreviousTask
      }//Go to the next RequestedTask
      return serviceTasks;

}
0
yaney00074
Asked:
yaney00074
  • 8
  • 5
1 Solution
 
nguyenvinhtuCommented:
Hi yaney00074,
If I run your function, serviceTasks = "12BN|45|RS34|00TY".
The process will go through this code snippet:

if(arPreviousTasks[j].StartsWith("12")
                    && arRequestedTasks[i].EndsWith("BN"))
               arPreviousTasks[j] = string.Concat("12", "BN") ;

About your solution, please give us your pseudo code or your idea in detail, so that we can help you. Something like:
What SHOULD the result be if you have the following:
previousTasks = "12BN|45|RS34|00TY"
requestedTasks = "12UTBN|09"
ServiceTaskDelimiter = "|";

0
 
yaney00074Author Commented:
Thanks for your feedback...

Basically, in a nutshell

In the database table there is a field called serviceTasks, that contains the information about what tasks where run previously...
They are stored in the follolwing fashion: 12BN45RS3400TY

During the cycle, this information  get retrieved and piped out   as follow: 12BN|45|RS34|00TY

Now 12, 12BN, 12XXBN is a particular service code:

12BN means has run a Credit report with a credit score
12 mean has run a credit report ONLY
12xxBN means has run a credit report from XX(EQ for EQuifax, TU for TransUnion, XP foreXPerian)
so xx may vary

so, let's say a client who use the application wants to change a criteria from 12BN to 12

Now I will have a new task to run, with 12 in the requestedTasks string 12| and other services

So my goal is not to have duplicate services on the same string (in this case, the serviceTasks)
I would like 12 to be inserted in the string serviceTasks and not 12BN,

Please let me know if you need futher information






the result would be 12BN|45|RS34|00TY|09
0
 
nguyenvinhtuCommented:
I need your help on this code snippet. Explain why you have 3 if here
/*if((arPreviousTasks[j] == "12") //preT = 12
                                          &&(!arRequestedTasks[i].StartsWith("12"))) //reqT <> 12
                                          arPreviousTasks[j] = arRequestedTasks[i];  //preT <> 12


                                    if(arPreviousTasks[j].StartsWith("12")  //preT = 12xx
                                          && arRequestedTasks[i].EndsWith("BN"))  //reqT = xxBN
                                          arPreviousTasks[j] = string.Concat("12", "BN") ; //preT = 12BN

 
                                    if((arPreviousTasks[j].StartsWith("12"))  //preT = 12xx
                                          && ((arRequestedTasks[i].StartsWith("12"))  //reqT = 12xx<>BN
                                          &&(!arRequestedTasks[i].EndsWith("BN"))))
                                          arPreviousTasks[j] = "12";*/ //preT = 12
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
nguyenvinhtuCommented:
PS: The symbol <> means NOT, for example 12xx<>BN means NOT end with BN.
Danny
0
 
yaney00074Author Commented:
My Code is kind of redundant; That's why I was looking for a more efficient way to do it.

To answer your question, the purpose of the function is (here's the pseudo code):
1. Grab previousTasks and requestedTasks strings coming into the function.
2. "Break" them down into tasks (that's why there is a delimiter, the | character)
3. Compare them one by one - in other words, get the current  "requested Task" and check if it exists in the "previous Task"
4a. ****if it doesn't exist, then add it to the "serviceTask" string (which will be returned to the calling function)
4b. ****If it does exist, then pass to the next "previous Task" item and so forth...

Now, here is where my problem comes in:
5. There SHOULD NOT be 2 instances of any of these: 12, 12BN, 12xxBN (with xx for EQ, TU, or XP) in the "serviceTask" string.

However when using two arrays, there's always a chance that, while a new ( 12, 12BN, 12xxBN) tasks from requestedTask is nneded to be run,
a 12, 12BN, or 12xxBN from previousTask slips through the comparison.

And it actually happened.

Note: If any of ( 12, 12BN, 12xxBN (with xx for EQ, TU, or XP) ) exist in both the "previous Task" and "requested Task"ONLY the one in "requested Task" should be added to the "serviceTask" string at the end.


I hope that's helps...
Please let me know if you need any further detail
0
 
nguyenvinhtuCommented:
In the Task, we have 12UTBN|09
12UTBTN : Service Code?
09 : Task Code?

And

preT = "12BN|45|RS34|00TY"
reqT = "12UTBN|09"

serT = "12UTBN|45|RS34|00TY|09"?
0
 
nguyenvinhtuCommented:
And, Is a Task always start with 12xx|...|... ?
0
 
yaney00074Author Commented:
You got it nguyenvinhtu,
However,

Anything in serT that is 12UTBN, or 12xxBN  (in shortstatrt with 12 and end with BN) should be reset to 12BN
and anything that is 12xx should be  reset to 12
anything 12 should remain 12


That's where I am stuck
0
 
nguyenvinhtuCommented:
Is there a case when
preT = "12UTBN|45|RS34|00TY"
reqT = "12UTBN|09"

Is it consider new Task?
0
 
nguyenvinhtuCommented:
I have a question, if this is what the result should be. And if the user continue to reqT, you just keep adding Task code of reqT ("09") to preT ("45|RS34|00TY"). For a while it may return a very long string in preT??? What is your idea???

preT = "12BN|45|RS34|00TY"
reqT = "12UTBN|09"

serT = "12UTBN|45|RS34|00TY|09"?
0
 
nguyenvinhtuCommented:
This is my solution, yaney. Check to see if it's your idea.
public static string InsertServiceTask(string previousTasks, string requestedTasks)
            {
                  //1 - Grab previousTasks and requestedTasks strings coming into the function.

                  //2 - "Break" them down into tasks (that's why there is a delimiter, the | character)
                  string ServiceTaskDelimiter = "|";
                  string[] arPreviousTasks = previousTasks.Split(ServiceTaskDelimiter.ToCharArray());
                  string[] arRequestedTasks = requestedTasks.Split(ServiceTaskDelimiter.ToCharArray());

                  string serviceTasks = "";

                  //3 - Compare them one by one - in other words, get the current  "requested Task" and check if it exists in the "previous Task"
                  // I keep all the Task code from previous Task. And add them to the service Task.
                  for (int i = 1; i < arPreviousTasks.Length; i++)
                  {
                        serviceTasks += arPreviousTasks[i];
                  }

                  //We start making comparison with Task Code, not Service Code, so start the loop from 1
                  for (int i=1; i<arPreviousTasks.Length; i++)
                  {
                        for (int j=1; j<arRequestedTasks.Length; j++)
                        {
                              // If Task code in the Request Task is new
                              if(arPreviousTasks[i].Equals(arRequestedTasks[j]) == false)
                              {
                                    serviceTasks += arRequestedTasks[j];
                              }
                        }
                  }

                  // Now process the Service Task.
                  if (arPreviousTasks[0].StartsWith("12"))
                  {
                        if (!arRequestedTasks[0].StartsWith("12"))
                        {
                              serviceTasks = arRequestedTasks[0] + serviceTasks;
                        }
                        else if(arRequestedTasks[0].StartsWith("12"))
                        {
                              if(arRequestedTasks[0].EndsWith("BN"))
                              {
                                    serviceTasks = "12BN" + serviceTasks;
                              }
                              else
                              {
                                    serviceTasks = "12" + serviceTasks;
                              }
                        }
                  }
                  return serviceTasks;
            }
0
 
yaney00074Author Commented:
Maybe this will help you understand thing even better.

Below is the original function:

serviceTask1 == 12BN|RD7|30|15
serviceTask2 == 09|12TUBN

public static string CombineServiceTask(string serviceTask1, string serviceTask2)
{
      string[] serviceTasks = serviceTask2.Split(ServiceTaskDelimiter.ToCharArray());

      for (int i=0; i<serviceTasks.Length; i++)
      {
            // Ensure that services are not duplicated
            if (serviceTask1.IndexOf(serviceTasks[i]) == -1)
            {
                  if (serviceTask1.Length == 0)
                        serviceTask1 = serviceTasks[i];
                  else
                        serviceTask1 += ServiceTaskDelimiter + serviceTasks[i];
            }
      }
      return serviceTask1;
}

This function worked up to a certain point, but 12TUBN will be added to  
serviceTask1 == 12BN|RD7|30|15 - which at the end (12BN|RD7|30|15|12TUBN) is not what I want

Note: I wanted 12TUBN to be changed to 12BN
0
 
yaney00074Author Commented:
Running your function with:
requestedTasks = "09|12TUBN"
previousTasks = "12BN|10RSfl|30|09|35"

this is what is being returned:
serviceTasks == "0910RSfl30093512TUBN12TUBN12TUBN12TUBN"

Which is not what I wanted... There are duplicates in the string

Only one instance of each task should be "attached" to the returning string

The result I tryng to get is 09|10RSfl|30|35|09|12TUBN (changed to 12BN)

NOTE: requestedTasks  should OVERRIDE previousTasks
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now