Powershell - get variable from each string in array

Viewed 193
$Number1Prt2 = "####   "
$Number1Prt3 = "  ##   "
$Number1Prt4 = "  ##   "
$Number1Prt5 = "  ##   "
$Number1Prt6 = "  ##   "
$Number1Prt7 = "###### " 

$Number2Prt1 = " #######  "
$Number2Prt2 = "##     ## "
$Number2Prt3 = "       ## "
$Number2Prt4 = " #######  "
$Number2Prt5 = "##        "
$Number2Prt6 = "##        "
$Number2Prt7 = "######### "

$Number3Prt1 = " #######  "
$Number3Prt2 = "##     ## "
$Number3Prt3 = "       ## "
$Number3Prt4 = " #######  "
$Number3Prt5 = "       ## "
$Number3Prt6 = "##     ## "
$Number3Prt7 = " #######  "

$Number4Prt1 = "##        "
$Number4Prt2 = "##    ##  "
$Number4Prt3 = "##    ##  "
$Number4Prt4 = "##    ##  "
$Number4Prt5 = "######### "
$Number4Prt6 = "      ##  "
$Number4Prt7 = "      ##  "

$Number5Prt1 = "######## "
$Number5Prt2 = "##       "
$Number5Prt3 = "##       "
$Number5Prt4 = "#######  "
$Number5Prt5 = "      ## "
$Number5Prt6 = "##    ## "
$Number5Prt7 = " ######  "

$Number6Prt1 = " #######  "
$Number6Prt2 = "##     ## "
$Number6Prt3 = "##        "
$Number6Prt4 = "########  "
$Number6Prt5 = "##     ## "
$Number6Prt6 = "##     ## "
$Number6Prt7 = " #######  "

$Number7Prt1 = "######## "
$Number7Prt2 = "##    ## "
$Number7Prt3 = "    ##   "
$Number7Prt4 = "   ##    "
$Number7Prt5 = "  ##     "
$Number7Prt6 = "  ##     "
$Number7Prt7 = "  ##     "

$Number8Prt1 = " #######  "
$Number8Prt2 = "##     ## "
$Number8Prt3 = "##     ## "
$Number8Prt4 = " #######  "
$Number8Prt5 = "##     ## "
$Number8Prt6 = "##     ## "
$Number8Prt7 = " #######  "

$Number9Prt1 = " #######  "
$Number9Prt2 = "##     ## "
$Number9Prt3 = "##     ## "
$Number9Prt4 = " ######## "
$Number9Prt5 = "       ## "
$Number9Prt6 = "##     ## "
$Number9Prt7 = " #######  "

$Number0Prt1 = "  #####   "
$Number0Prt2 = " ##   ##  "
$Number0Prt3 = "##     ## "
$Number0Prt4 = "##     ## "
$Number0Prt5 = "##     ## "
$Number0Prt6 = " ##   ##  "
$Number0Prt7 = "  #####   "

$IDNumber = "9873453"
$CharArray =$IDNumber.ToCharArray()

#Creating variables
foreach ($CharArray in [char[]]$CharArray) { $NumberArray += ("$"+"Number"+$CharArray+"Prt"+"1")}

basically what I am trying to achieve is foreach character in $IDNumber it will spit out a string along the lines of $number9prt1 and place it into a array, then move onto the next character in $IDNumber and so on.

when this is done I want to foreach string in the array get the variables for each of those string.

example the first one would look in variable $Number9Prt1 then $Number8Prt1 then $Number7Prt1 .....

Simply put, I have an array with values like Number9Prt1 then Number8Prt1 then Number7Prt1. Is there a way I can take a value from the array, convert it into a variable, grab the string from the variable and place that into a array.

I've been trying for days but no luck ... any help would be hugely appreciated.

Thank you

Novice coder, Luke

Reason: I use the console window at my workplace for a kind of "GUI" and having bigger text would be helpful.

1 Answers

I would personally just use arrays for this. Each index of the array can represent the number:

# For simplicity, I only created 0,1,2,3 indices.
# Each index number matches the graphical number it stores.
$Numbers = @("  #####   "," ##   ##  ","##     ## ","##     ## ","##     ## "," ##   ##  ","  #####   "),
           @("####   ","  ##   ","  ##   ","  ##   ","  ##   ","  ##   ","###### "),
           @(" #######  ","##     ## ","       ## "," #######  ","##        ","##        ","######### "),
           @(" #######  ","##     ## ","       ## "," #######  ","       ## ","##     ## "," #######  ")

$IDNumbers = '20012'
$IDNumbers.ToCharArray() | Foreach-Object { $Numbers["$_"] }

# Output
 #######
##     ##
       ##
 #######
##
##
#########
  #####
 ##   ##
##     ##
##     ##
##     ##
 ##   ##
  #####
  #####
 ##   ##
##     ##
##     ##
##     ##
 ##   ##
  #####
####
  ##
  ##
  ##
  ##
  ##
######
 #######
##     ##
       ##
 #######
##
##
#########

Explanation:

$Numbers is an array where its indexes each contain an array that represents a numerical graphic. So $Numbers['0'] contains array elements that make up the 0 graphic, $Numbers['1'] contains array elements that make up the 1 graphic, etc. Now you only need to retrieve the index of $Numbers that contains your target graphic.

If visual representation within the shell matters to you, you can still maintain that:

$Numbers = @(
"  #####   ",
" ##   ##  ",
"##     ## ",
"##     ## ",
"##     ## ",
" ##   ##  ",
"  #####   "
),
@(
"####   ",
"  ##   ",
"  ##   ",
"  ##   ",
"  ##   ",
"  ##   ",
"###### "
),
@(
" #######  ",
"##     ## ",
"       ## ",
" #######  ",
"##        ",
"##        ",
"######### "
),
@(
" #######  ",
"##     ## ",
"       ## ",
" #######  ",
"       ## ",
"##     ## ",
" #######  "
)

You can build on this approach for horizontal display:

$Numbers = @("  #####   "," ##   ##  ","##     ## ","##     ## ","##     ## "," ##   ##  ","  #####   "),
           @("####   ","  ##   ","  ##   ","  ##   ","  ##   ","  ##   ","###### "),
           @(" #######  ","##     ## ","       ## "," #######  ","##        ","##        ","######### "),
           @(" #######  ","##     ## ","       ## "," #######  ","       ## ","##     ## "," #######  ")

$IDNumbers = '20012'
for ($i = 0; $i -lt 7; $i++) {
    ($IDNumbers.ToCharArray() | Foreach-Object { $Numbers["$_"][$i] }) -join "`t"
}

You can simply just change the joining characters (tab here) to any one or group of characters. Since each graphic has 7 parts, you can loop through each part (indices 0-6). Since we created each part as an element of the array, you have an array of nested arrays. So graphic 0 part 1 would be $Numbers['0'][0], graphic 3 part 4 would be $Numbers['3'][3].


Edit: Working with Letters and Numbers

You can use a hash table instead of an array to hold your graphics. Then you can access each graphic array by the hash table key.

$hash = [ordered]@{}
$hash['A'] = @('      ####      ','     ##  ##     ','    ##    ##    ','   ##########   ','  ##        ##  ',' ##          ## ','##            ##')
$hash['0'] = @("  #####   "," ##   ##  ","##     ## ","##     ## ","##     ## "," ##   ##  ","  #####   ")
$hash['1'] = @("####   ","  ##   ","  ##   ","  ##   ","  ##   ","  ##   ","###### ")
$hash['2'] = @(" #######  ","##     ## ","       ## "," #######  ","##        ","##        ","######### ")
$hash['3'] = @(" #######  ","##     ## ","       ## "," #######  ","       ## ","##     ## "," #######  ")

$IDNumbers = 'A23012'
for ($i = 0; $i -lt 7; $i++) {
    ($IDNumbers.ToCharArray() | Foreach-Object { $hash["$_"][$i] }) -join "`t"
}
Related