How to pass new iteration value into array in a loop every nth element?

Welcome Forums General PowerShell Q&A How to pass new iteration value into array in a loop every nth element?

Viewing 7 reply threads
  • Author
    Posts
    • #211191
      Participant
      Topics: 41
      Replies: 113
      Points: 647
      Rank: Major Contributor

      I am trying to say for every 7/3 or 2 $someArray values ( $([math]::round($someArray.count/$bodyArray.count))), pass a new schedule to the restmethod, but for the life of me i cannot figure out how to do that…

      [/crayon]

      essentially, iteration1 will be:

      [/crayon]

      iteration2 will be  $bodyArray[schedule1]

      iteration3 would be

      [/crayon]

      and so forth

      this is also posted here for reference

    • #211200
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      If I got you right you could use a for loop and adjust the repetition interval to your needs. Something like this:

    • #211203
      Participant
      Topics: 41
      Replies: 113
      Points: 647
      Rank: Major Contributor

      If I got you right you could use a for loop and adjust the repetition interval to your needs. Something like this:

      <span class="ace_variable ace_instance">$someArray</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_string">'value1'</span>,<span class="ace_string">'value2'</span>,<span class="ace_string">'value3'</span>,<span class="ace_string">'value4'</span>,<span class="ace_string">'value5'</span>,<span class="ace_string">'value6'</span>,<span class="ace_string">'value7'</span>
      <span class="ace_variable ace_instance">$index</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_constant ace_numeric">0</span>
      <span class="ace_keyword">for</span>(<span class="ace_variable ace_instance">$index</span> ; <span class="ace_variable ace_instance">$index</span> <span class="ace_keyword ace_operator">-lt</span> <span class="ace_variable ace_instance">$someArray</span>.<span class="ace_identifier">Count</span> ; (<span class="ace_variable ace_instance">$Index</span> <span class="ace_keyword ace_operator">=</span> <span class="ace_variable ace_instance">$index</span> <span class="ace_keyword ace_operator">+</span> <span class="ace_constant ace_numeric">3</span>)) {
      <span class="ace_variable ace_instance">$someArray</span>[<span class="ace_variable ace_instance">$index</span>]
      }
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      i think there is a misunderstanding. what i am trying to say is:

      pseudocode:

      $frequency=0

      foreach ($val in $someArray)

      {

      $bodyArray ….

      for every 2 values (i.e. after 2nd element, after 4th element, etc. in $someArray) {$frequency++}

      invoke-restmethod …. -Body ($bodyArray[$frequency])

      }

    • #211206
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      Sorry, I cannot follow. Maybe it’s too late for me. 😉

    • #211212
      Participant
      Topics: 6
      Replies: 667
      Points: 97
      Helping Hand
      Rank: Member

      If I understand correctly, you are looking for something like this?

      Results:

    • #211215
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP
    • #211224
      Participant
      Topics: 6
      Replies: 667
      Points: 97
      Helping Hand
      Rank: Member

      Or if you need to be able to adjust your increment frequency

      Results:

    • #222195
      Participant
      Topics: 41
      Replies: 113
      Points: 647
      Rank: Major Contributor

      Or if you need to be able to adjust your increment frequency

      PowerShell
      19 lines

      <textarea class=”ace_text-input” style=”opacity: 0; height: 18px; width: 6.59781px; left: 51px; top: 0px;” spellcheck=”false” wrap=”off”></textarea>

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      $someArray = ‘value1’,‘value2’,‘value3’,‘value4’,‘value5’,‘value6’,‘value7’
      $frequency = 3
      $bodyArray = @()
      $current = 0
      $i = 1
      $someArray | ForEach-Object {
      If ($current -eq $frequency) {
      $current = 0
      $i++
      }
      $bodyArray += “schedule1=$i”
      $current++
      }
      $bodyArray
      #Invoke-RestMethod -UseDefaultCredentials -uri $($webPortalURL + “api/v2.0/CacheRefreshPlans($val)”) -Method Put -Body ($bodyArray[$([math]::round($someArray.count/$bodyArray.count))] | ConvertTo-Json -Depth 100) -ContentType “application/json”
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      Results:

      PowerShell
      7 lines

      <textarea class=”ace_text-input” style=”opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;” spellcheck=”false” wrap=”off”></textarea>

      1
      2
      3
      4
      5
      6
      7
      schedule1=1
      schedule1=1
      schedule1=1
      schedule1=2
      schedule1=2
      schedule1=2
      schedule1=3
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      ya something like that 😀

      but i ended up finding an easier way! i was overthinking it

      Just needed to use  %, the modulus operator:

      [/crayon]
Viewing 7 reply threads
  • The topic ‘How to pass new iteration value into array in a loop every nth element?’ is closed to new replies.