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: 39
      Replies: 109
      Points: 623
      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…

      $someArray = 'value1','value2','value3','value4','value5','value6','value7'
      
      $bodyArray = @()
      
      $i = 0
      
      foreach($val in $someArray)
      {
          $bodyArray += 'schedule1=$1','schedule2=$i','schedule3=$i'
      
          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"
      
          $i++
          $bodyArray = @() #reset the array
      }

      essentially, iteration1 will be:

      $bodyArray[schedule1]

      iteration2 will be $bodyArray[schedule1]

      iteration3 would be

      $bodyArray[schedule2]

      and so forth

      this is also posted here for reference

    • #211200
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      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:

      $someArray = 'value1','value2','value3','value4','value5','value6','value7'
      $index = 0
      for($index ; $index -lt $someArray.Count ; ($Index = $index + 3)) {
          $someArray[$index]
      }
    • #211203
      Participant
      Topics: 39
      Replies: 109
      Points: 623
      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:

      $someArray = 'value1','value2','value3','value4','value5','value6','value7'
      $index = 0
      for($index ; $index -lt $someArray.Count ; ($Index = $index + 3)) {
      $someArray[$index]
      }
      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: 4
      Replies: 2247
      Points: 5,484
      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?

      $someArray = 'value1','value2','value3','value4','value5','value6','value7'
      
      $bodyArray = @()
      $i = 0
      
      $someArray | ForEach-Object {
          If (-Not($bodyArray.Count%2)) {
              #Increment on Even
              $i++
          }
          $bodyArray += "schedule1=$i"
      }
      
      $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"

      Results:

      schedule1=1
      schedule1=1
      schedule1=2
      schedule1=2
      schedule1=3
      schedule1=3
      schedule1=4
    • #211215
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      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

      $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"

      Results:

      schedule1=1
      schedule1=1
      schedule1=1
      schedule1=2
      schedule1=2
      schedule1=2
      schedule1=3
    • #222195
      Participant
      Topics: 39
      Replies: 109
      Points: 623
      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:

      $someArray = 'value1','value2','value3','value4','value5','value6','value7'
      
      $bodyArray = @()
      
      $i = 0
      $nth = -1
      
      foreach($val in $someArray)
      {
          $bodyArray += 'schedule1=$i','schedule2=$i','schedule3=$i'
      
          Invoke-RestMethod -UseDefaultCredentials -uri $($webPortalURL + "api/v2.0/CacheRefreshPlans($val)") -Method Put -Body ($bodyArray[$nth)] | ConvertTo-Json -Depth 100) -ContentType "application/json"
      
          $i++
          if($i % ([math]::round($someArray.Count/$bodyArray.Count)) -eq 0) {$nth++}
          $bodyArray = @() #reset the array
      }
Viewing 7 reply threads
  • You must be logged in to reply to this topic.