比较一个字符串数组与另一个字符串数组。

huangapple go评论52阅读模式
英文:

Compare string of array to another string of array

问题

I'm trying to compare a string within an array to another string within another array

I feel like the logic is right but for some reason couldn't get the expected output

The array $groupslist has values of "cn-sg-robot-platform", "cn-sg-capture-platform", "cn-sg-easy-platform", "cn-sg-content-platform"

The array $comparestrings has values "sg-robot-platform" , "sg-easy-platform"

I want to print a message if $comparestring value is present in $groupslist ( if sg-robot-platform is present in cn-sg-robot-platform it should print a message )

The code which I tried isn't performing pattern search 比较一个字符串数组与另一个字符串数组。

$AdGroup = "cn-sg-robot-platform","cn-sg-capture-platform","cn-sg-easy-platform","cn-sg-content-platform"
$comparestrings = "sg-robot-platform" , "sg-easy-platform"

foreach ($item in $AdGroup) {
    $matchFound = $false
    foreach ($dev in $comparestrings) {
        if ($dev -like "*$item*") {
            $matchFound = $true
            echo "Match found"
            break
        }
    }
    if (-not $matchFound) {
        Write-Host "No match found for $item"
    }
}

**I want to print the match found when sg-robot-platform matches with cn-sg-robot-platform pattern **

英文:

I'm trying to compare a string within an array to another string within another array

I feel like the logic is right but for some reason couldn't get the expected output

The array $groupslist has values of "cn-sg-robot-platform","cn-sg-capture-platform","cn-sg-easy-platform","cn-sg-content-platform"

The array $comparestrings has values "sg-robot-platform" , "sg-easy-platform"

I want to print a message if $comparestring value is present in $groupslist ( if sg-robot-platform is present in cn-sg-robot-platform it should print a message )

The code which I tried isn't performing pattern search 比较一个字符串数组与另一个字符串数组。

$AdGroup = "cn-sg-robot-platform","cn-sg-capture-platform","cn-sg-easy-platform","cn-sg-content-platform"
$comparestrings = "sg-robot-platform" , "sg-easy-platform"

foreach ($item in $AdGroup) {
    $matchFound = $false
    foreach ($dev in $comparestrings) {
        if ($dev -like "*$item*") {
            $matchFound = $true
            echo "Match found"
            break
        }
    }
    if (-not $matchFound) {
        Write-Host "No match found for $item"
    }
}

**I want to print the match found when sg-robot-platform matches with cn-sg-robot-platform pattern **

答案1

得分: 1

以下是翻译好的代码部分:

  1. 遍历 $comparestrings 数组并测试每个值是否存在匹配项
$groupslist     = "cn-sg-robot-platform","cn-sg-capture-platform","cn-sg-easy-platform","cn-sg-content-platform"
$comparestrings = "sg-robot-platform", "sg-easy-platform"

foreach ($item in $comparestrings) {
    if ($groupslist -like "*$item*") {
        "找到匹配项:$item"
    }
}
  1. 使用 Where-Object 并遍历结果
$groupslist     = "cn-sg-robot-platform","cn-sg-capture-platform","cn-sg-easy-platform","cn-sg-content-platform"
$comparestrings = "sg-robot-platform", "sg-easy-platform"

$comparestrings | Where-Object { $groupslist -like "*$_*" } | ForEach-Object { "找到匹配项:$_" }
# 或者使用正则表达式 -match
# $comparestrings | Where-Object { $groupslist -match $_ } | ForEach-Object { "找到匹配项:$_" }

这两种方法都会输出:

找到匹配项:sg-robot-platform
找到匹配项:sg-easy-platform
英文:

Not quite sure what you intend to do here, but if you want to output where a match can be found, here's two possible solutions for you:

  1. loop over the $comparestrings array and test each value if there is a match
$groupslist     = "cn-sg-robot-platform","cn-sg-capture-platform","cn-sg-easy-platform","cn-sg-content-platform"
$comparestrings = "sg-robot-platform" , "sg-easy-platform"

foreach ($item in $comparestrings) {
    if ($groupslist -like "*$item*") {
        "Match found for $item"
    }
}
  1. use Where-Object and loop over the results
$groupslist     = "cn-sg-robot-platform","cn-sg-capture-platform","cn-sg-easy-platform","cn-sg-content-platform"
$comparestrings = "sg-robot-platform" , "sg-easy-platform"

$comparestrings | Where-Object { $groupslist -like "*$item*" } | ForEach-Object { "Match found for $_" }
# or use regex -match
# $comparestrings | Where-Object { $groupslist -match $item } | ForEach-Object { "Match found for $_" }

Both will output

Match found for sg-robot-platform
Match found for sg-easy-platform

huangapple
  • 本文由 发表于 2023年4月10日 19:36:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75976747.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定