Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop over array

Tags:

powershell

I need a piece of powershell-code to search and replace a certain string inside a text-file. In my example, I want to replace 23-06-2016' with '24-06-2016'. The script below does this job:

$original_file  = 'file.old'
$destination_file   = 'file.new'

(Get-Content $original_file) | Foreach-Object {
$_ -replace '23-06-2016', '24-06-2016' `
} | Out-File -encoding default $destination_file

As the search / replace string change I want to loop over an array of dates which might look like this:

$dates = @("23-06-2016","24-06-2016","27-06-2016")

I tried use the

$original_file  = 'file.old'
$destination_file   = 'file.new'

foreach ($date in $dates) {
  (Get-Content $original_file) | Foreach-Object {
  $_ -replace 'date', 'date++' `
  } | Out-File -encoding default $destination_file
}

In a first step, the date '23-06-2016' should be replaced by '24-06-2016' and in a second step, the date '24-06-2016' should be replaced by '27-06-2016'.

As my script is not working I am seeking for some advice.

like image 495
Phil U. Avatar asked Oct 12 '16 14:10

Phil U.


People also ask

How do you run a loop over an array?

How to Loop Through an Array with a forEach Loop in JavaScript. The array method forEach() loop's through any array, executing a provided function once for each array element in ascending index order. This function is known as a callback function.

Can you loop through an array?

You can loop through the array elements with the for loop, and use the length property to specify how many times the loop should run.

Which loop is used to iterate over arrays?

The for – of loop is for looping over data—like the values in an array.

What is meant by iterating over an array?

Iterating over an array means accessing each element of array one by one. There may be many ways of iterating over an array in Java, below are some simple ways. Method 1: Using for loop: This is the simplest of all where we just have to use a for loop where a counter variable accesses each element one by one.


1 Answers

You are using $date as your instance variable in your foreach loop but then referencing it as 'date', which is just a string. Even if you used '$date' it would not work because single-quoted strings do not expand variables.

Further, $date is not a number, so date++ would not do anything even it were referenced as a variable $date++. Further still, $var++ returns the original value before incrementing, so you would be referencing the same date (as opposed to the prefix version ++$var).

In a foreach loop, it's not very practical to refer to other elements, in most cases.

Instead, you could use a for loop:

for ($i = 0; $i -lt $dates.Count ; $i++) {
    $find = $dates[$i]
    $rep = $dates[$i+1]
}

This isn't necessarily the most clear way to do it.

You might be better off with a [hashtable] that uses the date to find as a key, and the replacement date as the value. Sure, you'd be duplicating some dates as value and key, but I think I'd rather have the clarity:

$dates = @{
    "23-06-2016" = "24-06-2016"
    "24-06-2016" = "27-06-2016"
}

foreach ($pair in $dates.GetEnumerator()) {
    (Get-Content $original_file) | Foreach-Object {
      $_ -replace $pair.Key, $pair.Value
    } | Out-File -encoding default $destination_file
}
like image 167
briantist Avatar answered Oct 05 '22 07:10

briantist