locked
Refresh foreach after adding an item RRS feed

  • Question

  • Hello,

    I'm iterating a list of objects using a foreach loop. During this loop if certain conditions are fulfilled I add another object to the iterated list. Here's a simple example of what I want to do:

    $numbers = 1,2,3,4,5
    foreach($num in $numbers){
    write-host $num
    if($num -eq 3){
    $numbers += 6
    }
    }

    So purely regarding the idea behind the script it's supposed to write 1,2,3,4,5,6 but it doesn't since the foreach loop doesn't seem to notice the change of the array. I already thought that foreach uses a kind of copy of the given array, but I didn't find a way to refresh it.

    I tried to relaunch the script after an item gets added, but that isn't really an option because the original script has about 10000 iterations which takes some time.

    Is there solution to this problem?

    Greetings,
    Bernd

    Thursday, December 10, 2015 8:40 AM

Answers

  • If the purpose of the script is to replicate files, there's no need to reinvent the wheel. Just use robocopy.

    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Bill_Stewart Sunday, December 20, 2015 4:17 PM
    • Marked as answer by Bill_Stewart Tuesday, January 12, 2016 9:05 PM
    Friday, December 11, 2015 1:17 AM

All replies

  • Hi Bernd,

    sure there is:

    Do a while loop with a counter. Your while condition is that while the counter is lower than the list ...

    $numbers = @(1,2,3,4,5)
    
    $n = 0
    
    while ($n -lt $numbers.Count)
    {
    
        if ($numbers[$n] -eq 4) { $numbers += 6 }
        $n++
    }

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Thursday, December 10, 2015 8:43 AM
  • Your question is vague. Why are you modifying the list of items you're trying to iterate?

    (How would PowerShell know when it's done iterating the objects?)


    -- Bill Stewart [Bill_Stewart]

    Thursday, December 10, 2015 10:38 AM
  • The script is about recurse syncronization and if a file got already copied to the sync-folder i want to update the file-list of the sync-folder since without it it's possible that a file gets copied there twice.
    Thursday, December 10, 2015 12:21 PM
  • I was aware of that possibilty and it's really working solution. I thought that powershell provides another more elegant way to do that :) .
    Thursday, December 10, 2015 12:23 PM
  • I was aware of that possibilty and it's really working solution. I thought that powershell provides another more elegant way to do that :) .

    Ah, in that case you could simply do a test-path within the loop, without changing the content, right?

    foreach ($file in $files)
    {
        if (Test-Path "$RootFolder\$($File.Name)") { continue }
        # ...
    }

    Cheers,
    Fred

    There's no place like 127.0.0.1

    Thursday, December 10, 2015 12:54 PM
  • If the purpose of the script is to replicate files, there's no need to reinvent the wheel. Just use robocopy.

    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Bill_Stewart Sunday, December 20, 2015 4:17 PM
    • Marked as answer by Bill_Stewart Tuesday, January 12, 2016 9:05 PM
    Friday, December 11, 2015 1:17 AM