N
N
Netnamee2019-03-01 16:54:30
PHP
Netnamee, 2019-03-01 16:54:30

How to get 5 elements from an array?

There is an array in it 23 elements, maybe more, maybe less
How to receive 5 elements and perform an action
That is, received 5 elements, displayed a message received
And so on until the end of the array.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
E
eRKa, 2019-03-01
@kttotto

C#
1.2 list.Skip(n).Take(5)
.

var slice = new ArraySegment<string>(list, n, n + 5)

3. You can also use a queue
var queue = Queue<string>() { ... };
var el1 = queue.Dequeue()
var el2 = queue.Dequeue()
var el3 = queue.Dequeue()
var el4 = queue.Dequeue()
var el5 = queue.Dequeue()

B
bkosun, 2019-03-01
@bkosun

Use the array_chunk function to split an existing array into several other N-element arrays.

S
Stalker_RED, 2019-03-01
@Stalker_RED

php.net/manual/en/function.array-chunk.php

$chunks = array_chunk($array, 5);
foreach($chunks as $chunk) {
  print_r($chunk);
}

E
Evgeny Popov, 2019-03-02
@awakewithadawn

View the remainder after dividing the increment by 5. If 0, then display your message.

V
Victor Semakin, 2019-03-03
@Chipsetone

The short answer, as always, is easily found on the Internet.

And my little fanatical monster in C # at 2 am looks like this =)
[TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var victimArray = new string[] {
                "1", "3", "2", "1", "23",
                "44", "32", "2", "10", "0",
                "17", "-1", "34", "79", };
            var expectedIndexesWithValues = new List<string>() {
                {"23"}, // "пятый" элемент
                {"0"} // следующий "пятый" элемент
            };

            var actualIndexesWithValues = new List<string>();

            victimArray.RunActionOnEach(eachElementNumber: 5, 
                actionOnElement: (elementValue) => {
                    Console.WriteLine($"Какое-то жизненно необходимое действие на элементе \"{elementValue}\"");
                    actualIndexesWithValues.Add(elementValue);
                }
            ).ToList();
            
            // проверяем, что результат совпадает с нашими ожиданиями
            CollectionAssert.AreEquivalent(expectedIndexesWithValues, actualIndexesWithValues);
        }
    }

    internal static class IEnumerableExtension
    {
        /// <summary>
        /// Дергает переданный Action на каждом eachElementNumber элементе
        /// </summary>
        public static IEnumerable<string> RunActionOnEach(this IEnumerable<string> sourceCollection, int eachElementNumber, Action<string> actionOnElement)
        {
            int currentNumber = 1;
            foreach (var element in sourceCollection)
            {
                if (currentNumber % eachElementNumber == 0)
                    actionOnElement(element);
                
                currentNumber++;

                yield return element;
            }
        }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question