Jump to content
Sign in to follow this  
Burgs

Extract numerics from a string?

Recommended Posts

Burgs

Greetings,

I wish to find/read a line within a textfile that will look very similar to this:

As = -3.86E+000 Ag = 2.41E+000

I've got this code taken from another application written in C# and I'd like to be able to "duplicate" it using AutoIt...I'm not sure if the same commands are available:

int Find_As = line.IndexOf("As =");

int Find_Ag = line.IndexOf("Ag =");

if((Find_As != -1) && (Find_Ag != -1))

{

int ADJUST = Find_Ag; //used to properly exclude "text" from "DIFF1"

Find_As += 4; Find_Ag += 4; //add 4 "positions" in line to exclude "text"

string DIFF1 = line.Substring(Find_As, ADJUST - Find_As);

string DIFF2 = line.Substring(Find_Ag);

} //Find_As AND Find_Ag NOT -1

Basically what I'm trying to do is to "strip out" ONLY the NUMERIC values from the string above so as to write those values out to a simple .txt file...

Share this post


Link to post
Share on other sites
Valuater

Maybe...

$string = "As = -3.86E+000 Ag = 2.41E+000"

$Split = StringSplit($string, "E")

$Result = ""
for $x =1 to $Split[0] -1
    $Info = StringRight($Split[$x], 4)
    $Result &= $Info & @CRLF
Next

MsgBox(0x0,"Result", $Result)

I am sure StrRegExpress is better, but this works too!

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites
PsaltyDS

Greetings,

I wish to find/read a line within a textfile that will look very similar to this:

As = -3.86E+000 Ag = 2.41E+000

I've got this code taken from another application written in C# and I'd like to be able to "duplicate" it using AutoIt...I'm not sure if the same commands are available:

int Find_As = line.IndexOf("As =");

int Find_Ag = line.IndexOf("Ag =");

if((Find_As != -1) && (Find_Ag != -1))

{

int ADJUST = Find_Ag; //used to properly exclude "text" from "DIFF1"

Find_As += 4; Find_Ag += 4; //add 4 "positions" in line to exclude "text"

string DIFF1 = line.Substring(Find_As, ADJUST - Find_As);

string DIFF2 = line.Substring(Find_Ag);

} //Find_As AND Find_Ag NOT -1

Basically what I'm trying to do is to "strip out" ONLY the NUMERIC values from the string above so as to write those values out to a simple .txt file...

StringRegExp() would be geekier, but this works fine:
$sString = "As = -3.86E+000 Ag = 2.41E+000"
If StringLeft($sString, 5) = "As = " Then 
    $iAg = StringInStr($sString, "Ag = ")
    If $iAg Then
        $DIFF1 = StringMid($sString, 6, $iAg - 6)
        $DIFF2 = StringMid($sString, $iAg + 5)
    EndIf
EndIf

MsgBox(64, "Results", "$DIFF1 = " & $DIFF1 & @CRLF & "$DIFF2 = " & $DIFF2)

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
Burgs

Thank you for the replies. Just so I understand:

Valuater your code basically cuts the string into 2 different strings "at the E" and then maniupulates the string a certain # of character positions to "weed out" the numbers only, correct?

And PsaltyDS you are searching for the "As = " and when found searching for the "Ag = " and then using the StringMid command to get positions to the right/left of those characters, correct?

Thanks again to both of you.

Share this post


Link to post
Share on other sites
Burgs

Thanks again!

Share this post


Link to post
Share on other sites
Sheri

Basically what I'm trying to do is to "strip out" ONLY the NUMERIC values from the string above so as to write those values out to a simple .txt file...

$line = "As = -3.86E+000 Ag = 2.41E+000"
$linevars = StringRegExp ($line, "(?i)As = ([0-9a-f.\-\+]+) Ag = ([0-9A-F.\-\+]+)", 1)
$AS=$linevars[0]
$AG=$linevars[1]
MsgBox(0,"Result","AS: " & $AS & @CRLF & "Ag: " & $Ag)

Share this post


Link to post
Share on other sites
Burgs

Thanks again, there seem to be quite a few ways of doing it...

Share this post


Link to post
Share on other sites
PsaltyDS

Thanks again, there seem to be quite a few ways of doing it...

There usually are! :)

Sheri gets the Geek-Brownie points for posting the RegExp solution! :)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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  

×