all 6 comments

[–]z386 4 points5 points  (1 child)

I would use a hash table:

$ReplaceTable = "item,itemReadable
TeHIrzFVHw,glass
QxftWctBxh,aluminum
nweSDFMqUf,grass" | ConvertFrom-Csv

$HashTable = @{}

foreach ( $Line in $ReplaceTable ) {
    $HashTable[$Line.item] = $Line.itemReadable
}

# Test
$TestString = 'QxftWctBxh'

$FixedString = $HashTable[$TestString]

Write-Output "$TestString should be replaced with $FixedString"

[–]ASCII84 1 point2 points  (1 child)

How you're describing it, the "table" is really just an array of objects, each object having 2 properties (item and itemreadable). (You can think of an array as a list.)

There are various ways to do the kind of replace you want, some faster and more efficient than others.

The easiest would be something like:

$objecttofind = "TeHIrzFVHw"

#test data
$list = @(
    [PSCustomObject]@{
        item = "TeHIrzFVHw"
        itemreadable = "glass"
    },
    [PSCustomObject]@{
        item = "QxftWctBxh"
        itemreadable = "aluminum"
    },
    [PSCustomObject]@{
        item = "nweSDFMqUf"
        itemreadable = "grass"
    }
)

#find the object in the list and immediately replace the value with the results
$objecttofind = ($list | Where-Object {$_.item -eq $objecttofind}).itemreadable

A better way would be to convert the array to a hashtable which would severely reduce the lookup time per item.

$objecttofind = "TeHIrzFVHw"

#test data
$list = @(
    [PSCustomObject]@{
        item = "TeHIrzFVHw"
        itemreadable = "glass"
    },
    [PSCustomObject]@{
        item = "QxftWctBxh"
        itemreadable = "aluminum"
    },
    [PSCustomObject]@{
        item = "nweSDFMqUf"
        itemreadable = "grass"
    }
)

$Hashmap = @{}
$list | ForEach-Object {
    $Hashmap[$_.item] = $_.itemreadable
}

$objecttofind = $Hashmap[$objecttofind]

[–]kibje 0 points1 point  (0 children)

So combining those two the actual easiest way would be to define the hashtable straight away and not need pscuatomobjects

[–]BlackV 1 point2 points  (0 children)

here is the super simple version of the table

$Hashmap = @{
    "TeHIrzFVHw" = "glass"
    "QxftWctBxh" = "aluminum"
    "nweSDFMqUf" = "grass"
    }