Jump to content

[Solved] How to make a two dimensional array from this data? - (Moved)


Recommended Posts

I have a dataset like this, (a strubg)

Username: User1

Type: Admin

RegDate: 1999


Username: User2

Type: User

RegDate: 2000

How to make a 2 dimensional array that I can display with _ArrayDisplay?

This would be a perfect 2D array to represent my data:

Username           Tpye        RegDate

User1              Admin       1999

User2              User        2000

  If you run this Powershell this powershell command, you can get this dataset that I am talking about:

Get-LocalUser | Select *

With this code you can try it to read into a string:

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>

#include "GUIListViewEx.au3"

#include <Array.au3> ; Just for display in example

#RequireAdmin
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseX64=y
#EndRegion

$sCommand = "powershell.exe Get-LocalUser | Select *"
Local $iPid = Run($sCommand, @WorkingDir , @SW_SHOW , $STDOUT_CHILD)
ProcessWaitClose($iPid)
Local $sOutput = StdoutRead($iPID)

ConsoleWrite($sOutput)

How can I correctly split $sOutput into a 2D array (with the above mentioned layout) that I can display and I work with?

Edited by DannyJ
Link to post
Share on other sites
  • Moderators

Moved to the appropriate forum, as the Developer General Discussion forum very clearly states:

Quote

General development and scripting discussions.


Do not create AutoIt-related topics here, use the AutoIt General Help and Support or AutoIt Technical Discussion forums.

Moderation Team

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to post
Share on other sites

Here SRE way :

#include <Array.au3>

Local $sOutput = "Username: User1" & @CRLF & _
@CRLF & _
"Type: Admin" & @CRLF & _
@CRLF & _
"RegDate: 1999" & @CRLF & _
@CRLF & _
@CRLF & _
"Username: User2" & @CRLF & _
@CRLF & _
"Type: User" & @CRLF & _
@CRLF & _
"RegDate: 2000"

; extract informations
Local $aArray = StringRegExp($sOutput, "(?|Username|Type|RegDate)\h*:\h*(.*)", 3)
_ArrayDisplay($aArray)

; convert 1d array into 2d array
Local $aFinal[UBound($aArray)/3][3]
For $i = 0 to UBound($aFinal) - 1
  $aFinal[$i][0] = $aArray[$i*3]
  $aFinal[$i][1] = $aArray[$i*3+1]
  $aFinal[$i][2] = $aArray[$i*3+2]
Next
_ArrayDisplay($aFinal)

 

Link to post
Share on other sites
9 hours ago, DannyJ said:

  If you run this Powershell this powershell command, you can get this dataset that I am talking about:

Get-LocalUser | Select *

Thats odd, if I run that in powershell i get totally different column names and data

AccountExpires         :
Description            : Built-in account for administering the computer/domain
Enabled                : False
FullName               :
PasswordChangeableDate :
PasswordExpires        :
UserMayChangePassword  : True
PasswordRequired       : True
PasswordLastSet        :
LastLogon              : 1/16/2021 12:30:28 AM
Name                   : Administrator
SID                    : S-1-5-21-4204884644-104046959-XXXXXXXXXX-500
PrincipalSource        : Local
ObjectClass            : User

 

Code hard, but don’t hard code...

Link to post
Share on other sites
9 hours ago, JockoDundee said:

Thats odd, if I run that in powershell i get totally different column names and data

AccountExpires         :
Description            : Built-in account for administering the computer/domain
Enabled                : False
FullName               :
PasswordChangeableDate :
PasswordExpires        :
UserMayChangePassword  : True
PasswordRequired       : True
PasswordLastSet        :
LastLogon              : 1/16/2021 12:30:28 AM
Name                   : Administrator
SID                    : S-1-5-21-4204884644-104046959-XXXXXXXXXX-500
PrincipalSource        : Local
ObjectClass            : User

 

Yes I wrote above data just for example, the correct data is the command

Link to post
Share on other sites
16 hours ago, DannyJ said:

Yes I wrote above data just for example, the correct data is the command

fwiw, you can modify the command 

Get-LocalUser | Select *

to

Get-LocalUser | Select * | Format-Table -HideTableHeaders | Out-File users.txt

This will put the output in table format, without headers and into a file.

Then you should be able to load the file using _FileReadFromArray() directly into a 2D array. - Untested.

Though you may have to mess with delimiters etc, depending on whether white space is embedded in the data.

Code hard, but don’t hard code...

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By TheAlienDoctor
      Hi, I was looking into creating a script that would detect if a file exists, then move it (and in some cases rename it, depending on the file) as well as write to a log file.
      The issue is, there is a lot of files that need to be moved, sometimes some files will exist and others won't depending on the use-case. However if a file does exist, it will always be going into the same directory with the same name.
      Currently I have an array nested inside of the array, with each array inside that array having both the old and new directory, and then a For loop to actually run through and do the file transferring. The issue I am having is how to call the Array inside of the array, because how do I specify  which the old directory is and which the new is?
      Global $FileTransfer[2000] = [Global $Dir1[2] = ["original dir 1", "new dir 1"], Global $Dir2[2] = ["original dir 2", "new dir 2"]] For $FileTransfer = [0] To [1] Step +1 If FileExists({original dir}) Then FileMove({original dir}, new dir, 1) FileOpen("log.latest.txt", 1) FileWrite("log.latest.txt", "{original dir} found, moved it to new dir." & @CRLF) FileClose("log.latest.txt") Else FileOpen("log_latest.txt", 1) FileWrite("log_latest.txt", "{original dir} not found, ignoreing it." & @CRLF) FileClose("log_latest.txt") EndIf Next I have put what I want the old and new directory to be for each array in {}, so hopefully its easier to tell which part is working and whats not.

      I am still reasonably new to AutoIT, any help is appreciated. Thankyou
    • By arunkw
      I have a spreadsheet - daily routine which has two columns: activity and time as shown here
      | Activity             | Time     | |----------------------|----------| | Sleep               |  6:00 am | | Toilet              |  6:15 am | | Get ready for gym  |  6:30 am | | Exercise            |  7:50 am | | ... more things      |  9:00 pm | | ... still more       | 10:45 pm | | Sleep               |  6:00 am |   I wanted to find out, say in C1 which activity is current for me using now() I.e., if it’s 6:45am on my watch, it should show me Exercise  in C1 Thanks to Adam D. PE, this formula works like magic to get the result =VLOOKUP(MOD(NOW(),1),{B2:B,A2:A},2,1)   Now, I want to reproduce same result in autoit, how to do that? To have easy solution say, I copy-paste spreadsheet data in array directly in code, right? Use for loop and run the above vlookup function and show the answer using tooltip. How to achieve this? please help.  
    • By goku200
      I have an Autoit script that lists files from a folder into an array list. Is there a way to separate the filenames by an underscore and include the id, version, name and date into separate columns in Excel.
      Example of filename:
      12345_v1.0_TEST Name [12345]_01.01.2022.html
      12345 would be in one column
      v1.0 would be in another column
      TEST Name [12345] would be in another column
      01.01.2022 would be in another column
      .html would be in another column
      Note: filenames always change each day.
      Here is my code that lists the files into column C and then writes the column Headers into Column D, E, F, G. Just need some help with separating them into columns by the _ delimiter
       
    • By mLipok
      Usually when I collect data from DataBase I need to give EndUser a possibility to select rows which should be taken in the processing loop.
      I was searching on the forum and I'm not able to find any UDF or even example of how to select data from array.
      I have my own solutions but I think they are not worth posting on the forum as it is very old code and I am looking for a better solution.

      Could anybody point me to some examples/solutions ?

      Thank you in advance.
      @mLipok
    • By EmilyLove
      I have a string containing the full path of an executable and an array of executables without their paths. I am trying to compare the string to the list in the array and if a match is found, remove it from the array. The entry get removed from the array successfully, and after checking its return result, uses it to update the ubound if it succeeded, but it doesn't want to update to the new value. Any ideas what I am doing wrong? It acts like it is read-only.
      #include <Array.au3> #include <File.au3> Local $sApp_Exe = "F:\App\Nextcloud\nextcloud.exe" Local $aWaitForEXEX = [3, "Nextcloud.exe", "nextcloudcmd.exe", "QtWebEngineProcess.exe"] For $h = 1 To $aWaitForEXEX[0] If StringInStr($sApp_Exe, $aWaitForEXEX[$h]) <> 0 Then $iRet = _ArrayDelete($aWaitForEXEX, $h) If $iRet <> -1 Then $aWaitForEXEX[0] = $iRet ;this line doesn't work. $aWaitForEXEX[0] doesn't update and shortly gives Error: Array variable has incorrect number of subscripts or subscript dimension range exceeded.: _ArrayDisplay($aWaitForEXEX) EndIf Next  
×
×
  • Create New...