Jump to content
sonic208

read string after space , litte bit compliated

Recommended Posts

Jfish

There are better ways using regular expressions but this is a possible method to grab what you are after with loops and arrays, you can assign the variables from the last array at the end:

include <Array.au3>

$string="P:4 |G:    1.2.0| L :   77770 |N:       0866746526"
$varArray=StringSplit($string,"|")
_ArrayDisplay($varArray); before any cleansing

for $a=0 to UBound($varArray)-1
    $varArray[$a]=StringStripWS($varArray[$a],8)
    $varArray[$a]=StringTrimLeft($varArray[$a],2)
Next

_ArrayDisplay($varArray); this one shows you the data to assign

 

  • Like 1

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites
kylomas

What do you do with "P:4"?


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
sonic208

P:4 is not needed , it can be ignored

but it can also be something like p:444 its not only one character, the other variables have always the same character count

Share this post


Link to post
Share on other sites
Melba23

sonic208,

This seems to work for the example string:

#include <Array.au3> ; Just to display result

#include <String.au3>
$sString = "P:4 |G:    1.2.0| L :   77770 |N:       0866746526"
$aRet = _StringBetween(StringStripWS($sString, $STR_STRIPALL) & "|", ":", "|")

_ArrayDisplay($aRet, "", Default, 8) ; Just to display result

M23


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
Malkey

Here are a couple of examples focusing on storing 1.2.0 in a variable  $g, 77770 in $l, and, 08.. in $n .

#include <Array.au3>

Local $a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, $n, $o, $p, $q, $r, $s, $t, $u, $v, $w, $x, $y, $z ; Copied from ConsoleWrite below.
Local $string = "G:    1.2.0| L :   77770 |P:4 |N:       0866746526"

; Method 1
$varArray = StringRegExp($string, "(?:(?:\A|\|)\h*([A-Z])\h*:\h+([.0-9]+)\h*)", 3) ; Create 1D array of variables and their values.
For $i = 0 To UBound($varArray) - 1 Step 2
    $varArray[$i] = StringLower($varArray[$i])
    Assign($varArray[$i], $varArray[$i + 1], 1) ; Assign value to lower case letter variable.
    ConsoleWrite('Eval("' & $varArray[$i] & '") = ' & Eval($varArray[$i]) & @CRLF) ; Display lower case letter variable and its value
Next
_ArrayDisplay($varArray)

; Or

;Method 2
Execute(StringTrimRight(StringRegExpReplace($string, "(?:\A|\|)\h*[A-Z]:[.0-9]+\h*|(?:(?:\A|\|)\h*([A-Z])\h*:\h+([.0-9]+)\h*)", 'Assign(StringLower("\1"), "\2", 1) & '), 3)) ; Assign value to lower case letter variable.
;ConsoleWrite(StringRegExpReplace($string, "(?:\A|\|)\h*[A-Z]:[.0-9]+\h*|(?:(?:\A|\|)\h*([A-Z])\h*:\h+([.0-9]+)\h*)", 'Assign(StringLower("\1"), "\2", 1) & ') & @CRLF) ; Shows the string that is trimmed, then executed.

;#cs ; ------ Display lower case letter variable and its value routine ------
$aVar = StringRegExp($string, "([A-Z ]+)(?=\:\h)", 3)
For $i = 0 To UBound($aVar) - 1
    $aVar[$i] = StringStripWS(StringLower($aVar[$i]), 8)
    ConsoleWrite('Eval("' & $aVar[$i] & '") = ' & Eval($aVar[$i]) & @CRLF)
Next
_ArrayDisplay($aVar)
;#ce ; ---- End of display lower case letter variable and its value routine ----
; This above routine can be replaced with the last ConsoleWrite("$g = " & $g & ...) if the "Local $a to $z" is at the top of this script.

; Additional info
Local $Loc = "Local "
For $i = 1 To 26
    $Loc &= "$" & Chr(96 + $i) & ", "
Next
ConsoleWrite(StringTrimRight($Loc, 2) & @CRLF) ; Copy to top of script.
;#cs
ConsoleWrite("$g = " & $g & @CRLF & _ ; Without the above "Local $a to $z" not being copied to top of script, this
        "$l = " & $l & @CRLF & _      ;  ConsoleWrite will not work.  The variables must be declared before the
        "$n = " & $n & @CRLF)         ;  Assign command is used. However Eval will work without the initial declaration.
;#ce

 

Share this post


Link to post
Share on other sites
mikell

My 2 cents - a little simpler

#include <Array.au3>

$sString = "P:4 |G:    1.2.0| L :   77770 |N:       0866746526"
; search a group of one or more dots and/or digits 
; preceded by G,L or N and a sequence of one or more non-digit chars
$res = StringRegExp($sString, '[GLN]\D+([\d.]+)', 3)
_ArrayDisplay($res)

 

Share this post


Link to post
Share on other sites
iamtheky

assigning values out of strings that are similar to the current example could be simple.

$sString = "P:4 |G:    1.2.0| L :   77770 |N:       0866746526"

$aStr = stringsplit(stringstripws($sString , 8) , "|" , 2)
for $i = 0 to ubound($aStr) - 1
If stringleft($aStr[$i] , 1) = "G" OR stringleft($aStr[$i] , 1) = "L" OR stringleft($aStr[$i] , 1) = "N" Then assign(stringleft($aStr[$i] , 1) ,  stringsplit($aStr[$i] , ":" , 2)[1])
Next

consolewrite(@CR & eval("G") & @CR & eval("L") & @CR & eval("N") & @CR)

 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites
Malkey

This example is based on a colon followed by a space after as the critical, decisive condition needed for selection of the variable and  its value. Then the script assigns the value to the lowercase variable.

#include <Array.au3>

Local $a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, $n, $o, $p, $q, $r, $s, $t, $u, $v, $w, $x, $y, $z
Local $sString = "G:    1.2.0| L :   77770 |P:4 |N:       0866746526"

; search a group of one or more dots and/or digits
; preceded by a ":" and one or more spaces.
$aValues = StringRegExp($sString, ':\h+([\d.]+)', 3)
_ArrayDisplay($aValues)

; Find the upper case letter that is before an
; optional space, a colon, ":",  and one or more spaces.
$aVars = StringRegExp($sString, '([A-Z])\h*:\h+', 3)
_ArrayDisplay($aVars)

For $ii = 0 To UBound($aValues) - 1
    Assign(StringLower($aVars[$ii]), $aValues[$ii])
Next

MsgBox(0,"Results", "$g = " & $g & @CRLF & "$l = " & $l & @CRLF & "$n = " & $n)

Note:  Method #2 of post #6 does this in one line.

Share this post


Link to post
Share on other sites
mikell

The same in one go for a precise answer to post #1
But as $g $n etc are already declared using Assign is not very useful

#include <Array.au3>

Local $g, $l, $n, $sString = "P:4 |G:    1.2.0| L :   77770 |N:       0866746526"

; search G, L or N followed by a sequence of one or more non-digit chars
; and then a group of one or more dots and/or digits
$aValues = StringRegExp($sString, '([GLN])\D+([\d.]+)', 3)
_ArrayDisplay($aValues)

For $i = 0 To UBound($aValues) - 1 step 2
    Assign(StringLower($aValues[$i]), $aValues[$i+1])
Next
MsgBox(0,"Results", "$g = " & $g & @CRLF & "$l = " & $l & @CRLF & "$n = " & $n)

 

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

×