Sign in to follow this  
Followers 0
SSzretter

Best method to parse out this string (to an array or similar)?

5 posts in this topic

#1 ·  Posted (edited)

Given the text below , I want to pull out all the lines that contain "Fan #" in to an array.  
 
 I assume I could use a substring type command and find the position of "Fan #", chop everything up to that point, then take the Fan line.   Then, repeat... 
 

I am wondering if there is a better approach? 

... lots of text (snip) ...

|  |  +- Voltage #8     :    2.144    2.144    2.144 (/lpc/it8720f/voltage/7)
|  |  +- VBat           :    3.328    3.328    3.328 (/lpc/it8720f/voltage/8)
|  |  +- Temperature #1 :       36       36       36 (/lpc/it8720f/temperature/0
)
|  |  +- Temperature #2 :       30       30       30 (/lpc/it8720f/temperature/1
)
|  |  +- Temperature #3 :       31       31       31 (/lpc/it8720f/temperature/2
)
|  |  +- Fan #1         :  1054.69  1054.69  1054.69 (/lpc/it8720f/fan/0)
|  |  +- Fan #2         :  1394.63  1394.63  1394.63 (/lpc/it8720f/fan/1)
|  |  +- Fan Control #1 :                            (/lpc/it8720f/control/0)
|  |  +- Fan Control #2 :                            (/lpc/it8720f/control/1)
|  |  +- Fan Control #3 :      100      100      100 (/lpc/it8720f/control/2)
|
+- NVIDIA GeForce 8500 GT (/nvidiagpu/0)
|  +- GPU Core       :      459      459      459 (/nvidiagpu/0/clock/0)
|  |  +- Fan #1         :  1054.69  1054.69  1054.69 (/lpc/it8720f/fan/0)
|  |  +- Fan #2         :  1394.63  1394.63  1394.63 (/lpc/it8720f/fan/1)

... lots of text (snip) ...
 
Edited by SSzretter

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

SSzretter,

No doubt an SRE guru will do it one line (I will have a go myself later :D) but you can do it like this:

#include <Array.au3>

; Simulate reading the file into an array with _FileReadToArray
$sString = "|  |  +- Voltage #8     :    2.144    2.144    2.144 (/lpc/it8720f/voltage/7)" & @CRLF & _
    "|  |  +- VBat           :    3.328    3.328    3.328 (/lpc/it8720f/voltage/8)" & @CRLF & _
    "|  |  +- Temperature #1 :       36       36       36 (/lpc/it8720f/temperature/0" & @CRLF & _
    ")" & @CRLF & _
    "|  |  +- Temperature #2 :       30       30       30 (/lpc/it8720f/temperature/1" & @CRLF & _
    ")" & @CRLF & _
    "|  |  +- Temperature #3 :       31       31       31 (/lpc/it8720f/temperature/2" & @CRLF & _
    ")" & @CRLF & _
    "|  |  +- Fan #1         :  1054.69  1054.69  1054.69 (/lpc/it8720f/fan/0)" & @CRLF & _
    "|  |  +- Fan #2         :  1394.63  1394.63  1394.63 (/lpc/it8720f/fan/1)" & @CRLF & _
    "|  |  +- Fan Control #1 :                            (/lpc/it8720f/control/0)" & @CRLF & _
    "|  |  +- Fan Control #2 :                            (/lpc/it8720f/control/1)" & @CRLF & _
    "|  |  +- Fan Control #3 :      100      100      100 (/lpc/it8720f/control/2)" & @CRLF & _
    "|" & @CRLF & _
    "+- NVIDIA GeForce 8500 GT (/nvidiagpu/0)" & @CRLF & _
    "|  +- GPU Core       :      459      459      459 (/nvidiagpu/0/clock/0)" & @CRLF & _
    "|  |  +- Fan #1         :  1054.69  1054.69  1054.69 (/lpc/it8720f/fan/0)" & @CRLF & _
    "|  |  +- Fan #2         :  1394.63  1394.63  1394.63 (/lpc/it8720f/fan/1)"
$aData = StringSplit($sString, @CRLF, 1) ; $STR_ENTIRESPLIT
; The file is now in an array

; Now loop through the array from the bottom up and delete unwanted lines
For $i = $aData[0] To 1 Step -1
    If Not StringInStr($aData[$i], "Fan #") Then
        _ArrayDelete($aData, $i)
    EndIf
Next

; And this is what we get
_ArrayDisplay($aData, "", Default, 8)
Please ask if you have any questions. :)

M23

Edit: I see that there is now no point in my working on an SRE! :D

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

Share this post


Link to post
Share on other sites

jguinch,

As my old maths teacher used to say: "An example for the student to complete". ;)

M23

1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

Share this post


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
Sign in to follow this  
Followers 0