Author Posts

January 24, 2018 at 5:45 pm

Hello Powershell geeks,
Its been a while since i touched powershell and i am trying to do something and i am completely clueless on how to achieve it, any help would be appreciated.
I have 30 numbers from 1 to 30 in a variable and i want to manipulate them and display as follows

1 1 1 1
1 1 1 2
1 1 1 3
1 1 1 4
1 1 1 5
1 1 1 6
1 1 1 7
1 1 1 8
1 1 1 9
1 1 1 10
1 1 2 2
1 1 2 3
1 1 2 4
1 1 2 5
1 1 2 6
1 1 2 7
1 1 2 8
1 1 2 9
1 1 2 10
1 1 2 11
1 1 3 3
and so on and need to make sure that it does not exceed 30 anywhere, it is actually 4 digits with the last one is where the change should start happening and the 3rd digit should change once the 4th digit reaches the 10th number from the first number of 4th digit and the 2nd digit should start changing once the 3rd digit reaches the dead end of 30, the start of the last digit depends on 3rd digit number as in bold font. is this really possible in powershell? i hope i am clear with my question

January 24, 2018 at 9:40 pm

What happens to 4th digit when it tries go past 30, does it stay at 30, or go back to 1?
Does the 3rd digit go from 1 to 30 then back again from 1 to 30, or does it go 1-30 then 2-31 then 3-32...
What's the range of digit 2, is it 1 to 10?
Does digit 1 stay as 1?
Making some assumptions:

foreach ($Slot2 in 1..10) { # Assuming digit 2 range is 1-10
    foreach ($Slot3 in 1..30) { # Assuming digit 3 goes 1-30 then 1-30 then 1-30...
        foreach ($Slot4 in $Slot3..($Slot3+9)) {
            if ($Slot4 -gt 30) { $Slot4 = 30 } # Assuming digit 4 stays at 30
            "1 $Slot2 $Slot3 $Slot4" # Assuming digit 1 is constant
        }
    }
}

January 25, 2018 at 2:07 am

What happens to 4th digit when it tries go past 30, does it stay at 30, or go back to 1?

It should go to 1

Does the 3rd digit go from 1 to 30 then back again from 1 to 30, or does it go 1-30 then 2-31 then 3-32...

Max number is 30 and once it reaches 30 it should go to 1

What's the range of digit 2, is it 1 to 10?

it is 1 to 30

Does digit 1 stay as 1?

No it should also go till 30

Thanks for your help, appreciate it :), i should be in constant touch with powershell 🙁

January 25, 2018 at 8:59 am

foreach ($Slot1 in 1..30) { 
    foreach ($Slot2 in 1..30) { 
        foreach ($Slot3 in 1..30) { 
            foreach ($Slot4 in $Slot3..($Slot3+9)) {
                if ($Slot4 -gt 30) { $Slot4Display = $Slot4 % 30 } else { $Slot4Display = $Slot4 }
                "$Slot1 $Slot2 $Slot3 $Slot4Display" 
            }
        }
    }
}

January 25, 2018 at 9:59 am

Thank you, it works. Awesome !!!

January 25, 2018 at 10:17 am

for each number i have a name , how can i assign that name to this number and i want the name to be displayed instead of number, for example

For this output 1 10 12 13 it should display as Sam Jon Adam Julie

January 25, 2018 at 12:31 pm

$myNames = @(
    'Emma','Liam','Noah','Olivia','Ava','Logan','Lucas','Sophia','Isabella','Mason',
    'Mia','Oliver','Amelia','Ethan','Charlotte','Elijah','Aiden','Harper','Ella','James',
    'Aria','Benjamin','Evelyn','Sebastian','Abigail','Jackson','Alexander','Emily','Avery','Jacob'
)
foreach ($Slot1 in 1..30) { 
    foreach ($Slot2 in 1..30) { 
        foreach ($Slot3 in 1..30) { 
            foreach ($Slot4 in $Slot3..($Slot3+9)) {
                if ($Slot4 -gt 30) { $Slot4Display = $Slot4 % 30 } else { $Slot4Display = $Slot4 }
                "$($myNames[$Slot1]) $($myNames[$Slot2]) $($myNames[$Slot3]) $($myNames[$Slot4Display])" 
            }
        }
    }
}

January 25, 2018 at 3:32 pm

Thanks again, works well, but the output for some reason cuts down last digit and displays separately 🙁

January 25, 2018 at 7:55 pm

Yeah, array elements are numbered 0..29 not 1..30, here's the fix:

$myNames = @(
    'Emma','Liam','Noah','Olivia','Ava','Logan','Lucas','Sophia','Isabella','Mason',
    'Mia','Oliver','Amelia','Ethan','Charlotte','Elijah','Aiden','Harper','Ella','James',
    'Aria','Benjamin','Evelyn','Sebastian','Abigail','Jackson','Alexander','Emily','Avery','Jacob'
)
foreach ($Slot1 in 1..30) { 
    foreach ($Slot2 in 1..30) { 
        foreach ($Slot3 in 1..30) { 
            foreach ($Slot4 in $Slot3..($Slot3+9)) {
                if ($Slot4 -gt 30) { $Slot4Display = $Slot4 % 30 } else { $Slot4Display = $Slot4 }
                "$($myNames[$Slot1-1]) $($myNames[$Slot2-1]) $($myNames[$Slot3-1]) $($myNames[$Slot4Display-1])" 
            }
        }
    }
}

January 31, 2018 at 11:40 am

Thanks again, it works 🙂