Trying to perform If contains on word list

This topic contains 10 replies, has 4 voices, and was last updated by  camelCreed 1 month, 1 week ago.

  • Author
    Posts
  • #77580

    Mike Smith
    Participant

    Hello,

    I have been trying to check if a value being checked is contained in a word list.

    I can get it working with just one word, but once I add the complete word list text file, it cannot find any matches.

    Here is an example of my code, just for testing purposes.

    $file = "Alabama","Alaska","Arizona","Arkansas","California"
    
    $stateList = Get-Content C:\temp\StateList.txt
    
    $data = $stateList.split(" ")
    
    $DB = $stateList
     foreach ($Data in $DB) {
     $state = $Data.Split()[0]
    }
    if ($state -contains $stateList) {
    Write-Host "Found State in List "
    } 
    # This is where I need help. $state contains California on first run and $stateList also has California, but it does not find a match. But if I hard code -contains "California", it works.
    # What I'm i missing here.
    

    Thanks for your help!

  • #77581

    Thom Schumacher
    Participant

    for starters your $state if is outside of your foreach loop. May try putting it inside the loop

    $DB = $stateList
     foreach ($Data in $DB) 
    {
     $state = $Data.Split()[0]
       if ($state -contains $stateList)
       {
        Write-Host "Found State in List "
       } 
    }
    
    • #77584

      Mike Smith
      Participant

      Hi Thom,

      I move it to inside the foreach loop, same issue. I have checked the variable and they all have the correct data.

      One thing that might be doing it is with the $stateList as it contains this:
      Alabama Alaska Arizona Arkansas California

      So, the -contains is not parsing this the way I think it is doing it.

      From what I can tell it looks like a long string value, separated by whitespace.

      Thanks

    • #77596

      js
      Participant
       $DB = $data
      

      I'm not really sure what you're doing, see if $file elements are in the file statelist.txt?

      $file = "Alabama","Alaska","Arizona","Arkansas","California"
      $stateList = Get-Content C:\temp\StateList.txt
      $data = $stateList.split(" ")
      foreach ($state in $file) {
        if ($data -contains $state) {
          Write-Host "Found State in List "
        } 
      }
      
    • #77616

      Mike Smith
      Participant

      Hi JS,

      I have a file with items one per line "StateList.txt" and need to check the foreach loop item, if it contains a match it will write it to the console.

      The $file holds the temp data to be checked, against the stateList file.
      $file = "Alabama","Alaska","Arizona","Arkansas","California"
      $stateList = Get-Content C:\temp\StateList.txt

      So, with your code example, if you create a stateList.txt with these states.
      Alabama
      Alaska
      Arizona
      Arkansas
      California

      Then, if you add say Florida to $file, but not stateList.txt it should not find it. And not write it to the console.

      What I'm seeing within my editor is that $file looks like one long string
      "Alabama Alaska Arizona Arkansas California Florida"

      Thanks

  • #77613

    camelCreed
    Participant
    $listA = "dog","cat","mouse","turtle","goat"
    
    $listB = "human","goat","bear","cat","tiger"
    
    ForEach ($l in $listA) {
    
    if ($listB -contains $l) {write-host "matched $l"}
    
    }

    This will show what you matched on if that helps. Result is:

    matched cat
    matched goat

    • #77619

      Mike Smith
      Participant

      Hi Creed,

      You confirmed my issue that I just posted.

      $listA = "dog","cat","mouse","turtle","goat"
      
      $listB = "human goat bear cat tiger"
      
      ForEach ($l in $listA) {
      
      if ($listB -contains $l) {write-host "matched $l"}
      
      }
      

      will not work since the variable looks like one big string.

      So I tried to split it in the listA side, but not on the listB side.

      If you can try this code and look in your editor the variable will be one big string, i also thought about select-string too.

      Thanks to all of you for your help,

      -Mike

    • #77626

      js
      Participant

      Why don't you split listb? select-string or match should work too

    • #77629

      camelCreed
      Participant

      js's code works.

      Here is the content/result of $statelist:

      Alabama
      Alaska
      Arizona
      Arkansas
      California

      Here is the script:

      $file = "Alabama","Alaska","Arizona","Arkansas","California","Florida"
      $stateList = Get-Content C:\stringtest\StateList.txt
      $data = $stateList.split(" ")
      foreach ($state in $file) {
        if ($data -contains $state) {
          Write-Host "Found $state in List"
        } 
      }

      Notice $file contains "Florida". Here is the result:

      Alabama
      Alaska
      Arizona
      Arkansas
      California
      Florida

      Result of the script:

      Found Alabama in List
      Found Alaska in List
      Found Arizona in List
      Found Arkansas in List
      Found California in List

      Weird that your .txt is not delimited(?) as it should be. Try adding a header row to it and import-csv instead of get-content.

  • #77635

    Mike Smith
    Participant

    Yes, looks to be working as i need it too.

    Thanks again for taking time to help me, really appreciate it.

    -Mike

    • #77646

      camelCreed
      Participant

      Awesome! Just a heads up. I was able to remove line 3 and change your if condition to $statelist -contains $state and it still works.

You must be logged in to reply to this topic.