Jump to content

SRER challenge


Deye
 Share

Recommended Posts

Every new paragraph ( from the second col - everything reading after  ": " ) must be converted into a new row.

$sVar = @LF & @CRLF _
         & 'MainWindowHandle : 132730' _
         & @LF & 'Id               : 8808' _
         & @LF & 'ProcessName      : SciTE' _
         & @LF & 'CPU              : 112.15625' _
         & @LF & 'Handles          : 512' & @LF & @CRLF
$sVar &= 'MainWindowHandle : 656932' _
         & @LF & 'Id               : 4268' _
         & @LF & 'ProcessName      : iexplore' _
         & @LF & 'CPU              : 0.15625' _
         & @LF & 'Handles          : 400' & @LF & @CRLF
$sVar &= 'MainWindowHandle : 1182078' _
         & @LF & 'Id               : 6748' _
         & @LF & 'ProcessName      : explorer' _
         & @LF & 'CPU              : 12.09375' _
         & @LF & 'Handles          : 1230' & @LF & @CRLF
$sVar &= 'MainWindowHandle : 1837220' _
         & @LF & 'Id               : 6868' _
         & @LF & 'ProcessName      : explorer' _
         & @LF & 'CPU              : 6.171875' _
         & @LF & 'Handles          : 552' & @LF & @CRLF

the total outcome after processing, the data should look like so:

$s = '' _
         & 'MainWindowHandle|Id|ProcessName|CPU|Handles' _
         & @LF & '132730|8808|SciTE|112.15625|512' _
         & @LF & '656932|4268|iexplore|0.15625|400' _
         & @LF & '1182078|6748|explorer|12.09375|1230' _
         & @LF & '1837220|6868|explorer|6.171875|552'

ConsoleWrite($s & @LF)

at looking for fewer lines of code as possible to do this right 

$s = StringRegExpReplace($sVar, "(\w+)(.{2,})(\R)", "$1|")
$s = StringRegExpReplace($s, "(.*\R)+(.*$)", "$1")
;~ ConsoleWrite($s & @LF)
$s &= StringRegExpReplace($sVar, "(.{2,}\: )(.{2,})(\R)", "$2|")
$s = StringRegExpReplace($s, "\R\R", "")
$s = StringRegExpReplace($s, "\|\R", @CRLF)
ConsoleWrite($s & @LF)

TIA

Edited by Deye
Link to comment
Share on other sites

what does 'SRER' mean?

... here my bark belch... :x

#include <String.au3>
#include <Array.au3>

$sVar = @LF & @CRLF _
         & 'MainWindowHandle : 132730' _
         & @LF & 'Id               : 8808' _
         & @LF & 'ProcessName      : SciTE' _
         & @LF & 'CPU              : 112.15625' _
         & @LF & 'Handles          : 512' & @LF & @CRLF
$sVar &= 'MainWindowHandle : 656932' _
         & @LF & 'Id               : 4268' _
         & @LF & 'ProcessName      : iexplore' _
         & @LF & 'CPU              : 0.15625' _
         & @LF & 'Handles          : 400' & @LF & @CRLF
$sVar &= 'MainWindowHandle : 1182078' _
         & @LF & 'Id               : 6748' _
         & @LF & 'ProcessName      : explorer' _
         & @LF & 'CPU              : 12.09375' _
         & @LF & 'Handles          : 1230' & @LF & @CRLF
$sVar &= 'MainWindowHandle : 1837220' _
         & @LF & 'Id               : 6868' _
         & @LF & 'ProcessName      : explorer' _
         & @LF & 'CPU              : 6.171875' _
         & @LF & 'Handles          : 552' & @LF & @CRLF


; ----------------
$sVar = StringReplace($sVar, @LF & @CR, @CR)
Local $aHeaders = _ArrayUnique(_StringBetween($sVar, @LF, ':'), 0, 0, 0, 0)
Local $s = _ArrayToString($aHeaders) & @LF
; _ArrayDisplay($aHeaders)

For $i = 0 To UBound($aHeaders) - 1
    $sVar = StringReplace($sVar, $aHeaders[$i] & ": ", "")
Next
$sVar = StringReplace($sVar, @LF, '|')

Local $aRecords = _StringBetween($sVar, @CR, @CR)

For $i = 0 To UBound($aRecords) - 1
    $s &= StringTrimLeft($aRecords[$i], 1) & @LF
Next

$s = StringTrimRight($s, 1)

ConsoleWrite($s & @LF) ; goal

; ----------------

_ArrayDisplay(_VarTo2D($s, "|"))

Func _VarTo2D($var, $sSeparator = @TAB)
    Local $aRows = StringSplit(StringStripCR($var), @LF), $aColumns, $aResult[$aRows[0]][1]
    For $iRow = 1 To $aRows[0]
        $aColumns = StringSplit($aRows[$iRow], $sSeparator)
        If $aColumns[0] > UBound($aResult, 2) Then ReDim $aResult[$aRows[0]][$aColumns[0]]
        For $iColumn = 1 To $aColumns[0]
            $aResult[$iRow - 1][$iColumn - 1] = $aColumns[$iColumn]
        Next
    Next
    Return $aResult
EndFunc   ;==>_VarTo2D

 

Edited by Chimp

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

For the fun, a one-liner :)

Edit : I saw 'challenge' in the title so I played the game. Obviously I don't recommend such a convoluted way :P

#include <Array.au3>

$sVar = @LF & @CRLF _
         & 'MainWindowHandle : 132730' _
         & @LF & 'Id               : 8808' _
         & @LF & 'ProcessName      : SciTE' _
         & @LF & 'CPU              : 112.15625' _
         & @LF & 'Handles          : 512' & @LF & @CRLF
$sVar &= 'MainWindowHandle : 656932' _
         & @LF & 'Id               : 4268' _
         & @LF & 'ProcessName      : iexplore' _
         & @LF & 'CPU              : 0.15625' _
         & @LF & 'Handles          : 400' & @LF & @CRLF
$sVar &= 'MainWindowHandle : 1182078' _
         & @LF & 'Id               : 6748' _
         & @LF & 'ProcessName      : explorer' _
         & @LF & 'CPU              : 12.09375' _
         & @LF & 'Handles          : 1230' & @LF & @CRLF
$sVar &= 'MainWindowHandle : 1837220' _
         & @LF & 'Id               : 6868' _
         & @LF & 'ProcessName      : explorer' _
         & @LF & 'CPU              : 6.171875' _
         & @LF & 'Handles          : 552' & @LF & @CRLF


$s =  StringTrimRight(StringRegExpReplace(StringSplit(StringStripWS($sVar, 3), @cr)[1], '\h*:\N+\n', "\|"), 1) &@cr& StringRegExpReplace(StringRegExpReplace(StringStripWS($sVar, 3), '(?m)^.+?:\h', "\|"), '((?<=\A|\v\v)\|)|\n', "")

Msgbox(0,"", $s)

Local $a[0][5]
_ArrayAdd($a, $s, 0, "|", @cr)

_ArrayDisplay($a)

 

Edited by mikell
Link to comment
Share on other sites

Thanks mikell,

I've noticed now that there are more various adjustments involved 

Here is the more authentic data input for this exercise :

#include <Array.au3>

_ArrayDisplay(_pInfoProc("scite.exe"))
_ArrayDisplay(_pInfoProc())

Func _pInfoProc($iPID = "") ; from name or pid
    $iPID = (IsInt($iPID) ? "-id " : $iPID ? "-name " : "") & StringRegExpReplace($iPID, "\..{3}", "")
    Local $sCommand = "Get-Process " & $iPID & "|Select-Object MainWindowHandle,Id,ProcessName,MainWindowTitle,Handles|Select -First 4"
    $iPID = Run(@ComSpec & " /c " & 'powershell -Command "' & $sCommand & '|Format-List"', "", @SW_HIDE, $stdout_child)
    ProcessWaitClose($iPID)
    Local $s = StdoutRead($iPID)
    Return _2DHeadersToArray($s)
EndFunc   ;==>_pInfoProc

Func _2DHeadersToArray($sVar = "", $sDelim = ':')
    Local $s = StringRegExpReplace(StringStripWS($sVar, 1), "(\w+)(.{2,})(\R)", "$1|")
    $s = StringSplit(StringStripWS($s, 3), @CR)[1]
    Local $s1 = StringRegExpReplace(StringStripWS($sVar, 1), '(\w+)(.{2,})(\R)', "$2|")
    $s &= @CRLF & $s1
    $s = StringRegExpReplace($s, "\|\R", @CRLF)
    Return _VarTo2D($s, "|", ":")
EndFunc   ;==>_2DHeadersToArray

; Thanks Chimp
Func _VarTo2D($var, $sSeparator = @TAB, $sDelim = ":")
    $var = StringStripWS($var, 3)
    Local $aRows = StringSplit(StringStripCR($var), @LF), $aColumns, $aResult[$aRows[0]][1]
    For $iRow = 1 To $aRows[0]
        $aColumns = StringSplit($aRows[$iRow], $sSeparator)
        If $aColumns[0] > UBound($aResult, 2) Then ReDim $aResult[$aRows[0]][$aColumns[0]]
        For $iColumn = 1 To $aColumns[0]
            $aResult[$iRow - 1][$iColumn - 1] = StringRegExpReplace(StringStripWS($aColumns[$iColumn], 3), $sDelim, "")
        Next
    Next
    Return $aResult
EndFunc   ;==>_VarTo2D

 

Edited by Deye
Link to comment
Share on other sites

@Deye : Here is another way : 

$s = StringRegExpReplace($sVar, "\s+:\s+", "|")
$cols = StringRegExpReplace($s, "(?s)^\R+|\|\K\N+\R(?!\R)||\|\N+\R{2,}.+", "")
$values = StringRegExpReplace($s, "\R\w+|\R\K\R\w+\||\R+$", "")
$result = $cols & $values
ConsoleWrite($result & @LF)

 

Edited by jguinch
Link to comment
Share on other sites

Thanks @jguinch

with my test, in one occasion your cols function stripped out a header

so so far this is what i have saved :

Func _2DHeadersToArray($sVar = "")
    Local $cols = StringRegExpReplace(StringRegExpReplace($sVar, "(\w+)(.{2,})(\R)", "|$1"), "(?m)\R\R|\R\|.*", "")
    Local $values = StringRegExpReplace(StringRegExpReplace($sVar, "(\w+)(.{2,})(\R)", "$2"), "[\h]+\:[\h]", "|")
    Local $s = StringStripWS($cols, 2) & @CRLF & StringStripWS($values, 3)
    $s = StringRegExpReplace($s, "(?m)^\|", "")
    ConsoleWrite($s & @LF & @LF)
    StringReplace($cols, "|", "|")
    Local $a[0][@extended]
    _ArrayAdd($a, $s)
    Return $a
EndFunc

 

Edited by Deye
Link to comment
Share on other sites

 

On 1/20/2022 at 2:07 PM, Deye said:

the total outcome after processing, the data should look like so:

$s = '' _
         & 'MainWindowHandle|Id|ProcessName|CPU|Handles' _
         & @LF & '132730|8808|SciTE|112.15625|512' _
         & @LF & '656932|4268|iexplore|0.15625|400' _
         & @LF & '1182078|6748|explorer|12.09375|1230' _
         & @LF & '1837220|6868|explorer|6.171875|552'

 

If you are going to execute a PowerShell command anyway, why not let Powershell do the heavy lifting in terms of formatting your output?

Example:

#include <Constants.au3>


example()

Func example()

    ;Command to get first 10 processes that have a window title and export
    ;the result to a pipe-delimited file.
    Const $PS_CMD  = "Get-Process" & _
                     "|Select-Object MainWindowHandle,Id,ProcessName,MainWindowTitle,Handles" & _
                     "|Where-Object {$_.MainWindowTitle -ne ''}" & _
                     "|Select -First 10" & _
                     "|Export-CSV -path .\~temp.csv -Delimiter '|' -NoTypeInformation"

    Local $iPid = 0, $iExitCode = 0
    Local $sCmdOutput = ""

    ;Execute the command
    $iPid = Run(StringFormat('powershell -command "%s"', $PS_CMD), "", Default, $STDERR_MERGED)
    ProcessWaitClose($iPid)
    $iExitCode  = @extended
    $sCmdOutput = StdoutRead($iPid)
    If $iExitCode Then Exit ConsoleWrite("ERROR: " & @CRLF & $sCmdOutput & @CRLF)

    ;Show output
    ConsoleWrite(StringReplace(FileRead("~temp.csv"), '"', '') & @CRLF)

    ;Delete temp file
    If FileExists("~temp.csv") Then FileDelete("~temp.csv")
EndFunc

Example Output:

MainWindowHandle|Id|ProcessName|MainWindowTitle|Handles
7669050|3932|firefox|SRER challenge - AutoIt General Help and Support - AutoIt Forums ? Mozilla Firefox|1117
13632660|2176|KeePass|KeePass.kdbx [Locked] - KeePass|229
15337406|4448|SciTE|C:\Projects\Personal\AutoIt\Test\A3Temp\a3_temp.au3 - SciTE [1 of 2]|197
18942332|4408|StikyNot|Sticky Notes|142

 

If you are using PowerShell 7.0+, you can use "-UseQuotes Never" in the Export-CSV.  Then, you won't need to do the StringReplace to removes the quotes.  I did it without -UseQuotes, in this example, so it would work using any versions of PowerShell.

Edited by TheXman
Link to comment
Share on other sites

@TheXman : what about asking Powershell to to the whole job ? (replacement of " and without temp file)

#include <Constants.au3>

Local $PS_CMD = "(Get-Process|where{$_.MainWindowTitle}|select MainWindowHandle,Id,ProcessName,MainWindowTitle,Handles -first 4|ConvertTo-Csv -NoTypeInformation -Delimiter '|')-replace '""""', ''"
$iPid = Run(StringFormat('powershell -command "%s"', $PS_CMD), "", @SW_HIDE, $STDOUT_CHILD )
ProcessWaitClose($iPid)
$sOut = StdoutRead($iPid)
ConsoleWrite($sOut)

 

Edited by jguinch
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...