question

Tobbel-9070 avatar image
0 Votes"
Tobbel-9070 asked MotoX80 answered

Remove single digit in a combination of digit letter

hi,

Hope someone can help me coming forward with this.

I have built a function that give back a string like this.
$class = 7c,9a,6a,9,8b

If single number exist (in this line 9) I need to remove it when exist in a combination of a letter a,b,c,d. The same if are single 7,6 or something.

I can’t remove all single letter. Should just be removed if exist in combination (number + letter).
The speed is important so hope find best way to do it. Thanks


windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered

Hi @Tobbel-9070

You can use a hash table for these sorts of things.

A hash table, also known as a dictionary or associative array, is a compact data structure that stores one or more key/value pairs.

Hash tables are frequently used because they are very efficient for finding and retrieving data. You can use hash tables to store lists and to create calculated properties in PowerShell.

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_hash_tables?view=powershell-7.1


--If the reply is helpful, please Upvote and Accept it as an answer--

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered RichMatheisen-8856 edited

I'm not sure your explanation is clear enough.

If there is a number followed by a letter (a - d) in the 2nd position in the list, do you want to remove BOTH the number and the letter? Or just the number, leaving the letter?

Do you want to do that for EVERY item in the list? Or just the pair in the 2nd position?

Using your example, what should be the result?

$class = 7c,6a,9,8b < remove the whole 2nd item ("9a")
$class = 7c,a,6a,9,8b < remove just the number from the 2nd item ("9a" becomes "a")
$class = c,a,a,9,b < remove each number that precedes a letter -- but don't alter an item that just consists of a single number
$class = 7c,6a,9,8b < remove the "9a" from the 2nd position if another "9" (without a letter) is found somewhere else in the list ("9" is just arbitrary -- any matching number will do)

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MotoX80 avatar image
0 Votes"
MotoX80 answered

Use a regular expression.

 cls
 $class = "7c,9a,6a,9,8b,A2,aa22,a11"
 "Input string:  {0}" -f $class
 $results =  ($class.split(',') -match "^[0-9][a-z]$") -join ","
 "Results.....:  {0}" -f $results


Note that it excludes capital letters and more than 2 characters.

146600-capture.jpg



capture.jpg (35.9 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.