Welcome Forums General PowerShell Q&A -lt comparison parameter

Viewing 6 reply threads
  • Author
    Posts
    • #171985
      Participant
      Topics: 8
      Replies: 7
      Points: 150
      Rank: Participant

      Hi Gents,

      Practicing on comparison parameters.

      I have a csv file that consists below contents.

      <colgroup><col style=”width: 48pt;” span=”3″ width=”64″ /> </colgroup>

      col1 col2 col3
      21 22 23
      31 32 33
      41 42 43
      a b c
      A B C
      AA bb CC
      aa BB cc
      Aa Bb cC

      Import-Csv C:\Users\Sugandhan\Desktop\Test.csv | Where-Object {$_.col1 -lt ‘3333’}

      If i run the above command it gives only below output

      col1 col2 col3
      —- —- —-
      21 22 23
      31 32 33

      Not the third Row (41). May i know the reason behind this?

      The same way -gt behaves.

      PS C:\> Import-Csv C:\Users\Sugandhan\Desktop\Test.csv | Where-Object {$_.col1 -gt ‘3333’}

      col1 col2 col3
      —- —- —-
      41 42 43
      a b c
      A B C
      AA bb CC
      aa BB cc
      Aa Bb cC

    • #171997
      Participant
      Topics: 1
      Replies: 20
      Points: 66
      Rank: Member

      I believe this would be because your imported data is a string and not an integer.  Strings sort differently, you can see this in the below test:

      PowerShell sees these numbers as integers and operates as you are expecting:

      41 -lt 3333
      True

      Now if we cast your first number as a string:

      [string]41 -lt 3333
      False

       

      You  could try casting your item as an integer, not able to test this so may need refined:

      Test.csv | Where-Object {[int]$_.col1 -lt '3333'}
    • #172000
      Participant
      Topics: 1
      Replies: 10
      Points: 100
      Helping Hand
      Rank: Participant

      Only one change would need to happen to this, because the original set of data are strings, errors will occur trying to convert letters (a, aa, AA, etc.) to a type Int32:

      Test.csv | Where-Object {[int]$_.col1 -lt '3333'}

      However, doing a Where-Object to do a regex match for Integers before casting should work:

      Test.csv | Where-Object{ $_.col1 -match '\d+'} | Where-Object {[int]$_.col1 -lt 3333}
    • #172003
      Participant
      Topics: 9
      Replies: 431
      Points: 736
      Helping Hand
      Rank: Major Contributor

      Instead of comparing strings, you can compare numbers:

      $Data = Import-Csv .\csv2.csv 
      $Data | Where-Object {$_.col1 -as [Int]}            # Integers Only
      $Data | Where-Object {-not ($_.col1 -as [Int])}     # Non-Integers Only
      $Data | Where-Object {($_.col1 -as [Int]) -and ($_.col1 -as [Int])-lt 3333} # Integers Less Than 3333
      $Data | Where-Object {($_.col1 -as [Int]) -and ($_.col1 -as [Int])-gt 3333} # Integers More Than 3333
      
      '33' -gt 3333 # False ==> comparing strings
      '34' -gt 3333 # True  ==> comparing strings
      33   -gt 3333 # False ==> comparing Integers
      34   -gt 3333 # False ==> comparing Integers
      
      '33' -gt '3333' # False ==> comparing strings
      '34' -gt '3333' # True  ==> comparing strings
      33   -gt '3333' # False ==> comparing Integers
      34   -gt '3333' # False ==> comparing Integers
      
    • #172267
      Participant
      Topics: 8
      Replies: 7
      Points: 150
      Rank: Participant

      Thanks All.

      Can anyone suggest any blog/document to learn about “when and where to use [int],[string],etc…?”

    • #172306
      Participant
      Topics: 9
      Replies: 431
      Points: 736
      Helping Hand
      Rank: Major Contributor
    • #172375
      Participant
      Topics: 8
      Replies: 7
      Points: 150
      Rank: Participant

      Thanks Again!

Viewing 6 reply threads
  • The topic ‘-lt comparison parameter’ is closed to new replies.