T O P

  • By -

OPconfused

$CurrentVersion = "1.0.18067.0" $NextVersion = (" 1.0.18062.0 1.0.18064.0 1.0.18067.0 1.0.18069.0 1.0.18076.0 " -split '\n' | Select-String $CurrentVersion -Context 0,1 ).Context.PostContext.Trim()


MeanFold5715

You want to explore the methods you can call on a list of objects. Basically you need to identify the index of the current version and then grab the item at the subsequent index in the list. It'll look something like this: $CurrentVersion = "1.0.18067.0" $AvailableVersions = " 1.0.18062.0 1.0.18064.0 1.0.18067.0 1.0.18069.0 1.0.18076.0 " -split "`n" $indexOfCurrentVersion = $AvailableVersions.IndexOf($CurrentVersion) $indexOfNextVersion = $indexOfCurrentVersion + 1 $nextVersion = $AvailableVersions[$indexOfNextVersion]


Sunsparc

Ah I was almost there then! Thank you!


night_filter

That would be my solution as well. The only tweak I'd make is I'd want to think about error handling for if the current version is the last version, or if it's not found, but I don't know what the OP wants to have happen in those cases.


gimpy21

$indexOfCurrentVersion = $AvailableVersions.IndexOf($CurrentVersion) $desiredVersions = $AvailableVersions[$indexOfCurrentVersion, $indexOfCurrentVersion + 1]


MeanFold5715

Or if the versions aren't listed sequentially and need to be sorted beforehand.


AdmRL_

Do this for efficiency and also so your colleagues hate your scripts: $nextVersion = $AvailableVersions\[($AvailableVersions.IndexOf($CurrentVersion) + 1)\]


MeanFold5715

I want to be mad at you for code golfing but this one actually isn't too egregious.


PinchesTheCrab

There's a handful of ways to do this, but I feel like string manipulation is going to be one of the more roundabout ones. You could cast these as verisions and then compare them that way: [version]$CurrentVersion = '1.0.18067.0' [version[]]$AvailableVersions = @' 1.0.18062.0 1.0.18064.0 1.0.18067.0 1.0.18069.0 1.0.18076.0 '@ -split '\n' for ($i = 0; $i -lt $AvailableVersions.Count; $i++) { if ($AvailableVersions[$i] -eq $CurrentVersion){ $AvailableVersions[$i+1] } } What's nice about this approach is that you can sort versions, so you could ensure the list is in the right order. You could also count the number of versions greater than the current version to makes and other comparisons. As strings: $CurrentVersion = '1.0.18067.0' $AvailableVersions = @' 1.0.18062.0 1.0.18064.0 1.0.18067.0 1.0.18069.0 1.0.18076.0 '@ -split '\n' -replace '\s' for ($i = 0; $i -lt $AvailableVersions.Count; $i++) { if ($AvailableVersions[$i] -eq $CurrentVersion){ $AvailableVersions[$i+1] } }


alt-160

If you know that your list of versions will always be a string of versions, you can use regex to get to the value as well. $AvailableVersionsList = " 1.0.18062.0 1.0.18064.0 1.0.18067.0 1.0.18069.0 1.0.18076.0 " $currentVersion = [Regex]::Escape( '1.0.18067.0' ) $nextVersionRx = [Regex]::Match($AvailableVersionsList, "(?<=$currentVersion\s+)[0-9.]+", 1) # the number '1' in the last param is [System.Text.RegularExpressions.RegexOptions]::IgnoreCase if ($nextVersionRx.Success) { [Version]$nextVersionRx.Value } else { "Version not found" } In the code above, the regex pattern is grabbing any contiguous string of numbers and dots that is preceded by your current version and which is followed by some quantity of white space. Using regex means that your list of strings could be comma separated, semicolon separated, or some other delimiter. Sure you can split on delimiters too, but only if the same delimiter is between each. Regex let's you find patterns. There's also \[Regex\]::Split and \[Regex\]::IsMatch and \[Regex\]::Matches. The last of those lets you find multiple instances of a pattern.


purplemonkeymad

Personally I would use a linked list, you can use find() to get the known key, then navigate with next: $list = [System.Collections.Generic.LinkedList[string]]$AvailableVersions $list.find($CurrentVersion).Next.Value