Remove single digit in a combination of digit letter

Tobbe l 1 Reputation point
2021-10-29T10:06:21.937+00:00

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
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,364 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Limitless Technology 39,356 Reputation points
    2021-11-04T19:01:11.34+00:00

    Hi @Tobbe l

    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://learn.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--

    0 comments No comments

  2. Rich Matheisen 44,776 Reputation points
    2021-11-04T19:36:55.103+00:00

    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)

    0 comments No comments

  3. MotoX80 31,571 Reputation points
    2021-11-04T19:45:12.263+00:00

    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

    0 comments No comments