Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do something for every hundredth element in an array

Tags:

arrays

c#

linq

I have 1 million elements in an array, I need to divide these elements into groups of 100, do a function and continue working on the next hundred

foreach (string value in lines)
{
    filescreated++;
    if (filescreated == ?????????)
    {
        do stuff 
    }
}

???? is equal to value divisible by 100

like image 376
Mark Watson Avatar asked Feb 18 '26 02:02

Mark Watson


1 Answers

is equal to value divisable by 100

foreach (...)
{
    filescreated++;

    if (filescreated % 100 == 0) 
    {
        // do stuff for the every 100th element
    }

    // do other stuff for every element
}

Reference: modulus (%) operator

Use this if you need to do something special for every 100th element, but you still need to process every element.

If you only need to process every 100th element, refer to Reed's answer.

like image 53
NullUserException Avatar answered Feb 19 '26 16:02

NullUserException



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!