Is this possible?

Welcome Forums General PowerShell Q&A Is this possible?

This topic contains 9 replies, has 2 voices, and was last updated by

 
Participant
9 months, 2 weeks ago.

  • Author
    Posts
  • #92119

    Participant
    Points: 0
    Rank: Member

    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

  • #92143

    Participant
    Points: 59
    Rank: Member

    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
            }
        }
    }
    
  • #92161

    Participant
    Points: 0
    Rank: Member

    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 🙁

  • #92174

    Participant
    Points: 59
    Rank: Member
    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" 
                }
            }
        }
    }
    
  • #92182

    Participant
    Points: 0
    Rank: Member

    Thank you, it works. Awesome !!!

  • #92185

    Participant
    Points: 0
    Rank: Member

    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

    • #92186

      Participant
      Points: 59
      Rank: Member
      $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])" 
                  }
              }
          }
      }
      
  • #92189

    Participant
    Points: 0
    Rank: Member

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

    • #92203

      Participant
      Points: 59
      Rank: Member

      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])" 
                  }
              }
          }
      }
      
  • #92491

    Participant
    Points: 0
    Rank: Member

    Thanks again, it works 🙂

The topic ‘Is this possible?’ is closed to new replies.