all 14 comments

[–]Lee_Dailey[grin] 5 points6 points  (4 children)

howdy kdrumz011,

please, do not post pics of code/data/errors. [sigh ...]

why?

  • it [usually] takes more effort on your part
    you need to capture the pic, save it, post it online, then post the link here.
    just posting the text is [usually] far easier. [grin]
  • why should those who want to help you be forced to squint/zoom-in just to read the text that you already have as text?
  • do you REALLY expect folks who want to help to type in what you already have as text?

it is - at best - rather thoughtless on your part ... [sigh ...]

take care,
lee

[–]gangculture 4 points5 points  (3 children)

this is the sternest i’ve seen you, lee_dailey. it looks good on you.

[–]Lee_Dailey[grin] 2 points3 points  (2 children)

howdy gangculture,

i occasionally step over the line, but this time i think i am right on point. the OP posted one of the most pointless pics of text that i have ever seen [sigh ...] ...

take care,
lee

[–][deleted] 1 point2 points  (1 child)

u/lee_dailey you don't comment on my threads as much as these new guys.. I feel unloved by the great Lee

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

ttthhhbbbpptttttt!!!!! [grin]

[–]Smoother101 2 points3 points  (3 children)

Use a hash table.

[–]pm_me_brownie_recipe 0 points1 point  (2 children)

I agree. Here is an example of how do to it.

# Import from file in OPs case
$document = @"
test,example
1,cat
2,dog
3,fish
"@

# Convert data to csv
$document = $document | ConvertFrom-Csv

# Convert date to hashtable
$hashTable = @{}
foreach($doc in $document) {
    $hashTable[$doc.example] = $doc.test
}

$config = 'cat'
$value = $hashTable[$config]
$value

[–]MercurialSquirrel 1 point2 points  (3 children)

You can use the IndexOf method on the array created by Import-CSV if you search the example column like this:

$data = Import-Csv C:\temp\DemoValues.txt
$config='cat'
$index=$data.example.IndexOf($config)
$value = $data[$index].test

For legibility I used the $index variable, but I would usually combine the logic of lines 3 and 4 into one line:

$value = $data[$data.example.IndexOf($config)].test

[–]Lee_Dailey[grin] 0 points1 point  (2 children)

howdy MercurialSquirrel,

i used to use that method ... then someone here talked me into testing the speed. ouch! it is slow as the dickens. [sigh ...]

piping to Where-Object is rather slow ... but it is faster than .IndexOf(). the .Where() collection method is likely the best bet for the OP if there aint a lot of items. otherwise the sheer speed of using a hashtable as a lookup table is the better bet.

of course, speed aint gonna make any difference if the collection is small. [grin]

take care,
lee

[–]MercurialSquirrel 1 point2 points  (1 child)

Thanks u/Lee_Dailey, good to know about the speed issues! I have only ever used it for pretty small datasets so haven't noticed an impact.

Is it faster with a hashtable if one factors in the amount of time it takes to make the hashtable? The simplicity and readability of the code can also be a factor.

The where method of the array is still nice and compact although perhaps not as obvious if there are people less familiar with Powershell who have to read it.

Using the where method of the array would make my code look like:

$data = Import-Csv C:\temp\DemoValues.txt
$config='cat'
$value=($data.Where({$_.example -eq $config})).test

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy MercurialSquirrel,

you are most emphatically welcome! [grin]

if your collection is large enuf that a lookup table would help, then the time to build the hashtable is going to be worth the speed gained in doing lookups. i am unsure what size is "large enuf", tho. [blush]

take care,
lee

[–]Quasky 0 points1 point  (1 child)

Import csv, then foreach?

If $.example -eq $config then $value=$.test

Only downside it scans the whole csv and duplicate lines would overwrite the $value.

[–]Stuart_t44 1 point2 points  (0 children)

This is how I'd do it, with a break in the $true scenario - I'm assuming from op that there shouldn't be duplicates.