howartp Posted January 22, 2018 Posted January 22, 2018 Hello. I'm trying to create what I would pre-powershell have called a multi-dimensional array but my own terminology may not be right. Google has loads of stackoverview questions but very few tutorials that don't jump in with initialisation using unary comma operators and type-objects that I've never heard of (and they don't explain)! I'm parsing multiple log files to locate (misplaced) iPads as they move around school. My desired output is a structure something like this: $results["09:00"]["iPad 1 roamed to IT Support"] $results["09:00"]["iPad 2 joined PE"] $results["09:00"]["iPad 5 roamed to IT Support"] $results["10:00"]["iPad 1 left Reception"] $results["11:00"]["iPad 1 roamed to IT Support"] $results["11:00"]["iPad 99 joined StaffRoom"] $results["12:00"]["iPad 12 authenticated PE"] I can then loop through and print the results thus: 09:00 iPad 1 roamed to IT Support iPad 2 joined PE iPad 5 roamed to IT Support 10:00 iPad 1 left Reception 11:00 iPad 1 roamed to IT Support iPad 99 joined StaffRoom 12:00 iPad 12 authenticated PE $time will always be something o'clock as the log files are hourly logs. I've got the parsing/looping that produces each piece of data $time and $activity. These aren't necessarily in any order as I'm parsing based on username and on any MAC address they're using, so I would normally say: $results.add($time,$activity) or $results[][] = ($time,$activity) or something on those lines. I DO need to maintain the order they were added to the array, within each time. Biggest problem appears to be I don't know the size of the $results array beforehand so powershell moans at me. Any thoughts on how to initialise/define and maintain such a structure, or alternative? (I've simplified the scenario slightly so the data might not quite make sense but run with me...) Peter
Steve21 Posted January 22, 2018 Posted January 22, 2018 Unless there's something in the "other" bit that means you can't, why not just increase the array on the fly? $results=@() $results+=(,("ipad1","room1","time1")) $results+=(,("ipad2","room2","time2")) $results+=(,("ipad3","room3","time3")) $results+=(,("ipad4","room4","time4")) $results Something like that should work won't be as fast or "smooth" are predefined one, but no reason shouldn't work Steve 1
howartp Posted January 23, 2018 Author Posted January 23, 2018 Hi Steve. Sincere apologies - I didn't see/notice a reply notification come through my emails yesterday so I've only just seen your reply. I've just come up with a solution this afternoon and came back to post it here for anyone else's future reference - as it turns out I think it's very similar to yours in practice, although written in more isolated steps. I'm particularly happy that it's in a readable format that I can save for future and easily recreate; if I used yours as is, I'd have to re-learn what the (,blah) syntax was doing. $macs = @{} #define an associative array (aka hash table) foreach ($i in 0..23) { $macs[$i] = @() #initialise an array for each hour as the key, eg $macs[12] is 12 } $macs[9] += "iPad 1 roamed to IT Support" #enter results in random order as they're parsed from logs $macs[10] += "iPad 1 left Reception" $macs[12] += "iPad 12 authenticated PE" $macs[9] += "iPad 2 joined PE" $macs[11] += "iPad 1 roamed to IT Support" $macs[11] += "iPad 99 joined StaffRoom" $macs[9] += "iPad 5 roamed to IT Support" #$macs Write-Host foreach ($j in 1..24) { if ($macs[$j].Count -gt 0) { Write-Host $j":00" -ForegroundColor Red Write-Host foreach ($mac in $macs[$j]) { Write-Host $mac } Write-Host } } This gives the following output, which is what I was after: [color="#FF0000"]9:00[/color] iPad 1 roamed to IT Support iPad 2 joined PE iPad 5 roamed to IT Support [color="#FF0000"]10:00[/color] iPad 1 left Reception [color="#FF0000"]11:00[/color] iPad 1 roamed to IT Support iPad 99 joined StaffRoom [color="#FF0000"]12:00[/color] iPad 12 authenticated PE Peter
Steve21 Posted January 23, 2018 Posted January 23, 2018 Ha no worries At least you got it sorted! Steve
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now