 |
|
| IMPORTANT: PowerShellCommunity.org is moving! - Wednesday, August 15, 2012PowerShellCommunity.org is moving! This community software, and the hardware that it sits on, are no longer serving the purposes of this community. As a result, we have decided to move this community to a new home at PowerShell.org. PowerShell.org is already up and running with the new community software and in its new location, so please post any new questions that you have on the forums over there instead of posting them on this site. We've already started getting some great questions from members of the community over there so please, come on over and join us!
While we are going through this transition, this site will remain up for the short term. New posts may no longer be created on these forums, however replies to existing posts are allowed so that users who posted questions don't have to re-post the same question on the new site.
[UPDATE 28/02/2013] New user registration has been disabled and forums have now been switched to read-only, including for existing posts since all threads that were started should now be completed. If you have a question about content on this site or about PowerShell in general, head over to PowerShell.org and ask it there where there are people actively using the site and answering questions.
If you have any questions, please let us know on the PowerShell.org site.
Thank you,
Kirk "Poshoholic" Munro |
|
| IMPORTANT: PowerShellCommunity.org is moving! - Wednesday, August 15, 2012PowerShellCommunity.org is moving! This community software, and the hardware that it sits on, are no longer serving the purposes of this community. As a result, we have decided to move this community to a new home at PowerShell.org. PowerShell.org is already up and running with the new community software and in its new location, so please post any new questions that you have on the forums over there instead of posting them on this site. We've already started getting some great questions from members of the community over there so please, come on over and join us!
While we are going through this transition, this site will remain up for the short term. New posts may no longer be created on these forums, however replies to existing posts are allowed so that users who posted questions don't have to re-post the same question on the new site.
[UPDATE 28/02/2013] New user registration has been disabled and forums have now been switched to read-only, including for existing posts since all threads that were started should now be completed. If you have a question about content on this site or about PowerShell in general, head over to PowerShell.org and ask it there where there are people actively using the site and answering questions.
If you have any questions, please let us know on the PowerShell.org site.
Thank you,
Kirk "Poshoholic" Munro |
|
|
|
|
import-csv where gt or lt
Last Post 29 Mar 2012 05:12 AM by jamma. 3 Replies.
|
Sort:
|
|
Prev Next |
You are not authorized to post a reply. |
|
jamma
 New Member Posts:29

 |
| 29 Mar 2012 02:46 AM |
|
Hi, I have a script which imports an CSV file, I tell it to import a certain header if -gt 1000 but the issue is that it still pulls the data which value is 499.9 $a = import-csv c:\test.csv | where {$_.capacityGBcal -gt 1000} I have attached the csv file, can anyone tell me why it wont compare properly?
|
test.zip
|
|
|
|
jamma
 New Member Posts:29

 |
| 29 Mar 2012 02:47 AM |
|
to add, my goal is to extract the data into three varibles $a, $b and $c.
$a = import-csv c:\test.csv | where {$_.capacityGBcal -lt 1000}
$b = import-csv c:\test.csv | where {$_.capacityGBcal -gt 1000 and -lt 2000}
$c = import-csv c:\test.csv | where {$_.capacityGBcal -gt 2001}
|
|
|
|
|
EBGreen
 Veteran Member Posts:1276

 |
| 29 Mar 2012 04:41 AM |
|
Everything out of the CSV by definition is a string. When you do a comparison of two values of different types in powershell, by default it tries to coerce the second value to be the same type as the first. So in this comparison: $_.capacityGBcal -lt 1000 Powershell compares this by coercing 1000 to be a string then does a string comparison. There are two potential ways to handle this. The first way is: $a = import-csv c:\test.csv | where {1000 -lt $_.capacityGBcal} In this case since 1000 is an int and it is first, the string will be coerced to an int and the comparison will happen the way that you expected. This will work, but in my opinion, the better method is: $a = import-csv c:\test.csv | where {[int]$_.capacityGBcal -lt 1000} In this case you are explicitly coercing the string to be an int. I just happen to prefer explicit conversions over hidden implicit ones. |
|
| "Look Ma...no strings!" |
|
|
jamma
 New Member Posts:29

 |
| 29 Mar 2012 05:12 AM |
|
Thanks EBGreen, I wasted some hours last night trying to get my head around the problem, but you have explained perfectly! all working.. regards, |
|
|
|
|
| You are not authorized to post a reply. |
|
Active Forums 4.3
|
|
 |