-5

I am doing some schema javascipt/C# coding and I have a question. I am doing breadcrumb schema for a little more reference. My goal is to find somehow to increment counter to be even to k in this example, so I can stop a comma from showing on the last iteration on the foreach loop. Right now, obviously they both increment at the same rate. I am having the biggest brain fart on where to place (counter++) to get it to increment and then end up even after the for each is completed. Both starting integer values should be as is. Just changing the counter to 1 is not what I am looking for :) Also the counter has to be in the ForEach loop.

Pseudo code below:

k = 1;
counter = 0;

foreach(string str in array1)
{
Schema Code
Schema Code
for(i = 0; i > 0; i++)
{
k++
counter++ (not right location, but reference)
}

if(k < counter)
{
print a coma
}
else if(k >= counter)
{
print space
}

}

Updated: My code would be where position is. I dont have access to my code at this moment. But the for each runs through the number on positions there are on the page. Then at the last position it will not write the comma.

 <script type="application/ld+json">
 {
"context": "http://schema.org",
"type": "BreadcrumbList",
"itemListElement": [{
"type": "ListItem",
"position": 5,
}
}
</script>
  • 1
    Are you looking for [String.Join](https://msdn.microsoft.com/en-us/library/57a79xd0(v=vs.110).aspx#Anchor_3)? – trailmax Aug 13 '18 at 23:52
  • 1
    If above duplicate does not work for your problem, please format your code, try to create a [mcve] and explain where exactly you have problem. – Christian Gollhardt Aug 13 '18 at 23:57
  • I updated a little better. I do not have access to my exact code at this moment, but it is just as the pseudo code. I am not looking for a String.Join. I am looking for a way to count the number of time it runs through the ForEach, then add a counter that will equal that counter to stop the comma from printing to screen. – ImSobesBoosted Aug 14 '18 at 00:16
  • It is not at all clear what you are doing or what the problem is, since all you're showing is pseudo code. For instance, why have two (actually three) different counters (`k`, `counter` and `i`)? You have an infinite loop in `for (i = 0; i > 0; i++)` (well, technically it'll bomb once you get to the maximum value of whatever type you use), and there seems to be little use. Finally, it appears as though you're just writing JSON, in which case you should use a JSON-specific writer (e.g., a serializer). – Heretic Monkey Aug 14 '18 at 00:23
  • @HereticMonkey Yeah it was terrible example. eulerspython understood where I was trying to get to and hit it on the nose. Thanks! – ImSobesBoosted Aug 14 '18 at 00:27

1 Answers1

0

Rather than having two separate counters, I recommend starting by taking the length of the array and using a counter to print a comma while the counter is less than the number of total items in the array. That way, when you finish you'll have one less comma than array item, and you won't have a comma at the end of what you've printed.

This is kind of involved but, since you really don't want to have the comma at the end of what you've printed, it may best fit your needs.

Here's some pseudocode based on what you wrote above:

// the length of the array needs to be a constant because depending on
// what your code does it may change the array length as your loop runs    
const arrayLength = the length of your array when you start 
int commaCount = 0

foreach(string str in array1)
{
  Schema Code
  Schema Code
  if (commaCount < (arrayLength -1)) 
  // (< arrayLength -1) because then your last comma will reach 
  // (arrayLength -1) and then not increase any more the last time around
  {
    print a comma
  }  
}

Let me know if that helps.

(Please disregard my syntax. This is pseudocode, so definitely don't copy and paste what I just wrote.)

eulerspython
  • 28
  • 1
  • 6