Convert string to int array in PowerShell - powershell

I'm trying to convert a string that looks like this,
2,3,4,5,6,20..30
to an array of integers. Here is the code I currently have:
[string]$a = "2,3,4,5,6,7"
[array]$b = $a.split(",")
[array]$c = foreach($number in $b) {([int]::parse($number))}
Which works, but not for the range of 20..30. How do I get that part working?

You can use the Invoke-Expression cmdlet to interpret the 10..30 bit, if the [int]::Parse() method call fails.
Here is a complete, working sample.
[string]$a = "2,3,4,5,6,7,10..30";
[array]$b = $a.split(",");
[array]$c = foreach($number in $b) {
try {
[int]::parse($number)
}
catch {
Invoke-Expression -Command $number;
}
}
$c;

One-liner (just for fun):
$c = "2,3,4,5,6,7,10..30".split(',') | % {iex $_}

Inside of a string, your .. is taken as-is and is not expanded to a range.
So $a = "1 .. 5" is actually 1 .. 5 and not 1, 2, 3, 4, 5.
To make your program work, you will have to tokenize not only on , but also on .. and then expand.

Related

Write to array every 4 characters

I have a string "1111222233334444" with numbers, how to make an array out of it,
"1111", "2222", "3333", "4444".
i want a 4 character instance to be instantiated, how is it implemented in powershell?
Simple loop would be enough:
$s = '1111222233334444'
$i = 0; while ($i -lt ($s.Length-3)/4) { $s.Substring($i*4, 4); $i++; }
(Length-3)/4 is for unaligned blocks of 4. Example: If string is 1111222 it gives (7-3)/4=1 full items.
In addition to comment by Olaf, another way is to use Regex. But, instead of using capturing groups and filtering:
$s -split "(....)" -ne ""
I would use:
[Regex]::Matches($s, "\d{4}").Value
Which seems more natural and readable.

How do I find and get value from multi dimensional array in powershell?

Basically I have an array that looks something like
areaCodes = # (
#("310", "LA"),
#("212", "NY"),
#("702", "LV")
)
I would like to have it so that for example if I have a variable $code = $212 Find if it is in the list, and if is, get the value associated with it.
Something like
if($areaCodes.contains($code))
{
WRITE-HOST $areaCodes[1]
}
and this would output NY
How do I do this in powershell? Or is there are more efficient way to do this?
You need to enumerate all arrays inside your array for this to work, for example:
($areaCodes | Where-Object { $_ -contains $code })[1] # => NY
Or using an actual loop:
foreach($array in $areaCodes) {
if($array -contains $code) {
$array[1]
break
}
}
But taking a step back, a hash table seems a lot more appropriate for your use case:
$code = 212
$areaCodes = #{
310 = "LA"
212 = "NY"
702 = "LV"
}
$areaCodes[$code] # => NY

How do I concatenate string and convert to upper case

first sorry for my English.
I't use powershell but I have to make a script to generate some SSRS's reports. I need to concatenate two variables and convert the result to uppercase, the code below works fine but I't know convert the result to uppercase and put it, for example, into other variable. Thanks a lot
$direxcel = "C:\Users\Administrador\Documents\SSRS"
$rdl = "Sin Título"
write-host ${direxcel}\$rdl.xls
To concatenate strings you can use several options.
$a = "first string"
$b = "second string"
$c = "$a $b"
or simply (but not necessarily recommended)
$c = $a + $b
To make something upper case.
$c = "$a $b".ToUpper()

adding numbers returned from function in powershell

I have the following basic code in powershell where I'm calculating the value of x
function add3([int]$num) {
return ($num + 3);
}
[String]$argA = "AB";
[int]$x = (add3($argA.Length) + 2);
Write-Host($x)
Running this in ISE I'm getting the value 5 instead of 7.
In PowerShell, you call functions by listing the arguments separated by spaces, not commas, and parentheses are not used. This is a common source of confusion. Your expression:
[int]$x = (add3($argA.Length) + 2);
Is a call to the function add3 with three arguments: ($argA.Length) and the strings + and 2. Since your function has only one argument, the result is 2 + 3 = 5 and the other two are discarded (calling a function with extraneous parameters is not an error in PowerShell). You can verify this is what's happening by changing your function:
function add3([int]$num, $y, $z) {
Write-Host $y;
Write-Host $z;
return ($num + 3);
}
The solution is to parenthesize the expression properly:
$x = (add3 $argA.Length) + 2;
You're getting the correct output.
Try to use this approach instead:
[int]$x = (add3($argA.Length)) + 2
function add3($num) {
return ($num + 3);
}
[String]$argA = "AB";
$x = (add3($argA.Length)) +2
Write-Host($x)

How can I treat a string as a sequence of hexadecimal values in PowerShell?

I have a string of characters in PowerShell like so:
Encoded: A35C454A
I want to treat each character as a hexadecimal value. In Ruby, this is as simple as Encoded[0].hex. How can I do this in PowerShell?
Simples:
[Convert]::ToInt32($encoded[0], 16);
(ToInt16 can be used too, but the built-in Int type is actually a shorthand for Int32)
please have a look
function asciiToHex($a)
{
$b = $a.ToCharArray();
Foreach ($element in $b) {$c = $c + "%#x" + [System.String]::Format("{0:X}",
[System.Convert]::ToUInt32($element)) + ";"}
$c
}
http://learningpcs.blogspot.in/2009/07/powershell-string-to-hex-or-whatever.html