/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" PowerShell : How can I tell whitch numbers are mis... "," PowerShell Active Directory Browser teaser "," Powershell, Has my Dell a dangerous battery Part 2 ? "," PowerShell, Has my Dell a dangerous battery ? "," PowerShell Adding -verbose switch to functions "," PowerShell and Active Directory Part 9 (nested gro... "," Powershell and hey Scripting guy ! "," PowerShell Orphan share remover Tool (Update) "," PowerShell and Active Directory Part 8 (ACL's) "," Signing PowerShell Scripts "," ")

Saturday, September 02, 2006

 


PowerShell : Can you do that less cryptic ?



Ok,Oh, all right

the onliner from last post (" PowerShell : How can I tell witch numbers are missing from a sequence of numbers found in a text file ?  ,

gc test.txt | sort {[int]$_} |% {$i = 1}{while ($i -lt $_){$i;$i++};$i++}

was maybe a bit to tiny to post without any explanation.

you can do very powerfull things on the commandline like this, but for in a script or example this might not very handy.

 I still not going to explain it,

but I will make it a lot more Clear what Happens, by showing that you do not have too be so tiny in PowerShell , you can write this in a more verbose and readable way , as the former one-liner Can also be written as Follows :

 

# Read numbers from file 

$numbers = get-content test.txt

# Sort the Numbers

$Sortednumbers = $Numbers | Sort {[int]$_}

# start comparing numbers in file to counter

$counter = 1

foreach ($number in $Sortednumbers ) {

  # as long as Number in file is higher than counter, output as missing and raise counter  

  while ($counter -lt $Number){
    "$Counter is missing"
     $Counter++
  }

  # counter is the same as Number, hence is in file output and raise counter

  "$Counter is In file"
  $Counter++

}

 This code does exactly the same as the on-liner, is a bit more work, but much more clear for code you or somebody else might have to read or change.

*Edit* some other suggestions given on IRC for handling this question are :

 

# Other suggestions on IRC
 
# <dreeschkind> 

$n = gc test.txt
$n|%{[int]$max = 1}{if ($max -lt $_){$max=$_}}
diff -r $(1..$max) $n

# <mow001> slower

$n = gc test.txt
$max = ($n | Measure-Object -max).maximum
diff -r $(1..$max) $n

# <d2d> faster ?
 
$result = @(); $arr = Get-Content test.txt; for ($i = 1; $i -lt $arr[-1]; $i++) { if ($arr -notcontains $i) { $result += $i } }; $result

*Edit 2*  Another tip : Need for speed, use another cool feature from Powershell go .NET directly : 

[int[]]$a=[io.file]::ReadAllLines('c:\powershell\test.txt')
[array]::sort($a)

Enjoy,

Greetings, /\/\o\/\/
Tags : Monad msh PowerShell




Comments: Post a Comment



<< Home

Archives

October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006  

$Links = ("PowerShell RC1 Docs"," PowerShell RC1 X86"," PowerShell RC1 X64"," Monad GettingStarted guide"," Monad Progamming Guide"," Monad SDK"," Monad videos on Channel 9"," MSH Community Workspace"," scripts.readify.net "," MonadSource"," www.reskit.net"," PowerShell Blog"," Under The Stairs"," computerperformance powershell Home"," proudlyserving"," MSH on wikipedia"," MSHWiki Channel 9"," Keith Hill's Blog"," Precision Computing"," PowerShell for fun"," MSH Memo (Japanese)"," monadblog")

find-blog -about "PowerShell","Monad" | out-Technorati.
find-blog -contains "","" | out-Technorati.
Google
 
Web mow001.blogspot.com

This page is powered by Blogger. Isn't yours?