1

With most code I've seen, a Powershall array cell is accessed as $arrayName[1], ie the cell number is "hard coded" as a number. I have tried $arrayName[$required_cell] ... but it will not access the data.

Here's what I am trying to do. The initial string: $k="012345xxxxxxyyyyyyABC123zzzzzztttttt" This is divided into an array of 6 sets of 6 numbers via a for loop

$tmp = Get-Content -Encoding UTF8 "$md\k" ($md is a path, 'k' is about 100 letters)
$k=$tmp.Substring(0,36)
for ($i = 0; $i -lt 36; $i += 6) {
    $newstring+=$k.substring($i,6)   
}

I then access the first cell, and break it into invidivual digits

$tmp=$newstring[0] 
$sp=$tmp.Split('')
user    ## This a a function where I try to access $sp[3]

(I tried using $sp=$tmp, but that gave a strange sequence showing " 0[]1[]2[]3[]4[]5", ** [] being a space. Looking at Format-Hex showed this as "0\D\A1\D\A" etc. The Split does at least create one number per cell)

The theory is that I can use those digits to access the newstring array, thus $sp[3] - which contains the number 3 - would allow me to access the third cell in $newstring, ie $newstring[$sp[3]] would return "ABC123"

The function user() isn't important as there is nothing in there that works as it cannot find these codes; $newstring[3] works fine!

The six digits shown in the first cell are generated randomly, so that the data in the string moves around. So for example, if the code was "035241" then at $sp[3] we now have "2", so the data would now be found in cell 2, ie 035241xxxxxABC123tttttyyyyyzzzzz

So the question is: Can you use a variable to access an array's cell data, or is there some other method I should use (ArrayList?)

3
  • this >>> $newstring+=$k.substring($i,6) <<< builds a string. then this >>> $tmp=$newstring[0] <<< grabs the 1st letter of that string. then you try to split that letter with >>> $sp=$tmp.Split('') <<< ///// that makes no sense at all.
    – Lee_Dailey
    Nov 22 '20 at 12:04
  • It seems to work: In the loop, it jumps forward six numbers at a time, and gets a string or length 6. It then pushes into an array, (forgot to mention $newstring=@() at start of script). So then newstring[0] references the first cell in array ... I have now "partially" solved this now; I had to put the variable in double quotes, thus $tmp=$newstring["$sp[3]"] ... only problem now is that it's being returned as " ABC123" (ie whitespace in front) Nov 22 '20 at 12:31
  • ah! you need to add that you set the $Var to be an array. otherwise your code makes no sense at all. [grin]
    – Lee_Dailey
    Nov 22 '20 at 15:47
0

Spliting a sting on an empty string freturns the string:

PS C:\>>'035241'.Split('')
035241

To get an array of characters, you want to use casting:

PS C:\> [Char[]]'035241'
0
3
5
2
4
1

But when we try to use these as indices into an array, casting as [Int] returns the ASCII code:

PS C:\>>[Int[]][Char[]]'035241'
48
51
53
50
52
49

We have to cast the [Char] array as a [String] array, where subsequent casting as [Int] works as expected:

[Int[]][String[]][Char[]]'035241'
0
3
5
2
4
1

So you want something like this:

$NewString = @()
$k="012345xxxxxxyyyyyyABC123zzzzzztttttt"
for ($i = 0; $i -lt 36; $i += 6) {
    $newstring+=$k.substring($i,6)   
}

$tmp=$newstring[0]

$sp = [Int[]][String[]][Char[]]$tmp

$NewString[$sp[3]]
4
  • Nice answer, many thanks. I did "upvote", but as I have reputation of less than 15, it doesn't show. (Please don't take this next part as a "I-Know-Better") I managed to get the result I wanted with "$tmp=$newstring[0] ## Loads first array $sp=$tmp ## splits $tmp into cells of $sp $tmp=$sp[1] ## value of $sp[1] assigned to tmp $f=$newstring["$tmp"]" ## Gets value from $newstring No idea HOW it's doing it ... but it works !! Nov 22 '20 at 17:19
  • Because you're performing the same set of transformations as I showed you, but in a more obscure way. Copy this code: $tmp = $newstring[0] ; $tmp.GetType() ; $sp = $tmp[3] ; $sp.GetType() ; "$sp".GetType() Enclosing the [Char] in quotes casts it as a string. Then using the string as an array index casts that string as an integer: $NewString["$sp"]. Nov 22 '20 at 17:47
  • I forgot that at the start of the script, I have set it to use ArrayList. Apparentely, that should use "Add" rather than "+=" to build the array. But now I'm trying to get the reverse to work, put the code BACK into a random string ... but this time the $newstring("$tmp") trick doesn't work. So will try "Add" and then "Clear" array - or even $outstring to see if either work. Nov 22 '20 at 18:13
  • How you build the array of strings is inconsequential to your initial question. You need to understand why my answer worked. Nov 22 '20 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.