0

I'm wondering if it's possible to do something like the code below... obviously I haven't found the right way to do it so I'm looking for a corrections.

What I'm trying to do is go through a variable that contains all our players' best scores on each of the 18 holes on our course in order to count all types of scores for each hole.

The code doesn't return anything yet, that's normal, it's because what I wrote doesn't seem to work at all :

#Define Pars
$par1 = [int]4
$par2 = [int]5
$par3 = [int]4
$par4 = [int]3
$par5 = [int]4
$par6 = [int]4
$par7 = [int]3
$par8 = [int]5
$par9 = [int]4
$par10 = [int]4
$par11 = [int]4
$par12 = [int]5
$par13 = [int]4
$par14 = [int]3
$par15 = [int]4
$par16 = [int]3
$par17 = [int]4
$par18 = [int]5

#Set score type counts to 0
$hole = [int]1

while ($hole -in 1..18) {

$albatrosCount[$hole] = [int]0
$eagleCount[$hole] = [int]0
$birdieCount[$hole] = [int]0
$parCount[$hole] = [int]0
$bogeyCount[$hole] = [int]0
$doublebogeyCount[$hole] = [int]0
$worseCount[$hole] = [int]0
$hole = $hole + 1

}

#Collect statistics
foreach ($entry in $eclecticResults) {

$statsList = "" | Select-Object "Type","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","Totals"

#albatros
$hole = [int]1

while ($hole -in 1..18) {

    $albatrosCount[$hole] = $albatrosCount[$hole] + $albatrosCount[$hole]
    if ($entry.$hole -eq $par[$hole] - 3) {$albatrosCount[$hole] = $albatrosCount[$hole] + 1}
    $hole = $hole + 1

}

#eagle
$hole = [int]1

while ($hole -in 1..18) {

    $eagleCount[$hole] = $eagleCount[$hole] + $eagleCount[$hole]
    if ($entry.$hole -eq $par[$hole] - 2) {$eagleCount[$hole] = $eagleCount[$hole] + 1}
    $hole = $hole + 1

}

#birdie
$hole = [int]1

while ($hole -in 1..18) {

    $birdieCount[$hole] = $birdieCount[$hole] + $birdieCount[$hole]
    if ($entry.$hole -eq $par[$hole] - 1) {$birdieCount[$hole] = $birdieCount[$hole] + 1}
    $hole = $hole + 1

}

#par
$hole = [int]1

while ($hole -in 1..18) {

    $parCount[$hole] = $parCount[$hole] + $parCount[$hole]
    if ($entry.$hole -eq $par[$hole]) {$parCount[$hole] = $parCount[$hole] + 1}
    $hole = $hole + 1

}

#bogey
$hole = [int]1

while ($hole -in 1..18) {

    $bogeyCount[$hole] = $bogeyCount[$hole] + $bogeyCount[$hole]
    if ($entry.$hole -eq $par[$hole] + 1) {$bogeyCount[$hole] = $bogeyCount[$hole] + 1}
    $hole = $hole + 1

}

#doublebogey
$hole = [int]1

while ($hole -in 1..18) {

    $doublebogeyCount[$hole] = $doublebogeyCount[$hole] + $doublebogeyCount[$hole]
    if ($entry.$hole -eq $par[$hole] + 2) {$doublebogeyCount[$hole] = $doublebogeyCount[$hole] + 1}
    $hole = $hole + 1

}

#worse
$hole = [int]1

while ($hole -in 1..18) {

    $worseCount[$hole] = $worseCount[$hole] + $worseCount[$hole]
    if ($entry.$hole -gt $par[$hole] + 2) {$worseCount[$hole] = $worseCount[$hole] + 1}
    $hole = $hole + 1

}

}

How can I use "$hole" to name variables so I don't have to "while" 18 times per type and per entry ?

6
  • Oh - I know the part where I add up for counts isn't correct either, I'm really just looking for how I should correct my $variable[$otherVariable] naming problem. Commented Aug 27, 2024 at 12:58
  • Possible duplicate of stackoverflow.com/q/13015303/17372809 Commented Aug 27, 2024 at 13:28
  • @Dima Technically you are correct, but IMHO the use of Arrays and Hashtables is a better solution here, i.e. why $par1 ... $par18 when an array with 18 elements is a better fit. And why is it always $xxxCount[$hole] = $xxxCount[$hole] + $xxxCount[$hole] ? And what is in $eclecticResults? Why so many whiles when one would be sufficent? Commented Aug 27, 2024 at 13:43
  • Yes, the $xxxCount[$hole] + $xxxCount[$hole] part is completely incorrect, I haven't corrected that yet because I need to solve the other problem... And indeed, arrays and hashtables are what I'm now going for - realized that too actually by reading my own question again... $eclecticResults contains a table of 375 players with each of their best result on each of the 18 holes. Commented Aug 27, 2024 at 15:03
  • 1
    What does your source “player scores” data look like? There’s probably an easier way to aggregate it with group-object it than hand-counting all the scores in a bunch of while loops… Commented Aug 27, 2024 at 15:14

2 Answers 2

1

you can use Set-Value and Get-Value to get variables dynamically

1..18 | ForEach-Object { Set-Variable -Name "par$_" -Value $_ }

$par4 # returns 4

$x=8
Get-Variable -Name "par$x" -ValueOnly # returns 8
Sign up to request clarification or add additional context in comments.

Comments

0

Alright - so probably not the most elegant way of doing it but I did find a solution. Indeed, by using hashtables instead of trying to play with variable names...

#Create statistics

#Prepare statistics table
$statistics = @()

#Prepare export CSV for stats path
$exportStatsCSVPath = "H:\Docs\MensDay\statistics\LLNStats2024_$date.csv"

#Define Pars
$par = "" | Select-Object "1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18"
$par.1 = [int]4
$par.2 = [int]5
$par.3 = [int]4
$par.4 = [int]3
$par.5 = [int]4
$par.6 = [int]4
$par.7 = [int]3
$par.8 = [int]5
$par.9 = [int]4
$par.10 = [int]4
$par.11 = [int]4
$par.12 = [int]5
$par.13 = [int]4
$par.14 = [int]3
$par.15 = [int]4
$par.16 = [int]3
$par.17 = [int]4
$par.18 = [int]5

#Prepare counts
$albatrosHole = "" | Select-Object "Type","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18"
$albatrosHole.Type = "Albatros"

$eagleHole = "" | Select-Object "Type","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18"
$eagleHole.Type = "Eagle"

$birdieHole = "" | Select-Object "Type","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18"
$birdieHole.Type = "Birdie"

$parHole = "" | Select-Object "Type","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18"
$parHole.Type = "Par"

$bogeyHole = "" | Select-Object "Type","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18"
$bogeyHole.Type = "Bogey"

$doubleBogeyHole = "" | Select-Object "Type","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18"
$doubleBogeyHole.Type = "Double Bogey"

$worseHole = "" | Select-Object "Type","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18"
$worseHole.Type = "Worse"

#Collect statistics
foreach ($entry in $eclecticResults) {

$hole = [int]1

while ($hole -in 1..18) {

    if ($entry.$hole -eq $par.$hole - 3) {$albatrosHole.$hole = $albatrosHole.$hole + 1}
    if ($entry.$hole -eq $par.$hole - 2) {$eagleHole.$hole = $eagleHole.$hole + 1}
    if ($entry.$hole -eq $par.$hole - 1) {$birdieHole.$hole = $birdieHole.$hole + 1}
    if ($entry.$hole -eq $par.$hole) {$parHole.$hole = $parHole.$hole + 1}
    if ($entry.$hole -eq $par.$hole + 1) {$bogeyHole.$hole = $bogeyHole.$hole + 1}
    if ($entry.$hole -eq $par.$hole + 2) {$doubleBogeyHole.$hole = $doubleBogeyHole.$hole + 1}
    if ($entry.$hole -gt $par.$hole + 2) {$worseHole.$hole = $worseHole.$hole + 1}
    $hole = $hole + 1

}

}

$statistics += $albatrosHole
$statistics += $eagleHole
$statistics += $birdieHole
$statistics += $parHole
$statistics += $bogeyHole
$statistics += $doubleBogeyHole
$statistics += $worseHole

So yeah... I probably worked around my lack of knowledge but at least this works... Anything you can teach me from this point is welcome of course but make sure you don't waste time you don't have for me.

2 Comments

I suggest you to make a new question: your solution has deviated from the original one.
Correct. Can someone maybe delete this question entirely ? Can I ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.