Jump to content

Convert String to Integer


Recommended Posts

If you're positive that each line is a number, then use Number($string) and spare the If StringIsInt test.

Also _FileReadToArray may be better suited to a bulk processing.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

in this example it creates a file with 3 lines , parses each for just the number and displays each. You can dump those to any other format (array, text file) and then set them to variables.

**and i was basing the string off your last thread, assuming these were related queries

#include 

$file = fileopen (@scriptdir & "/test.txt" , 2)

$string = "sadf14.01dkhfasjk" & @CRLF
$string &= "asdkjfa6785asdgka" & @CRLF
$string &= "fghdkfh78dfkgksdjkalf"

filewrite ($file , $string)
fileclose ($file)

$max = _fileCountLines (@scriptdir & "/test.txt")


for $i = 1 to $max

$fileread = filereadline (@scriptdir & "/test.txt" , $i)

$reg = stringregexp($fileread , "(\d+\.?\d?\d?)" , 1)

msgbox(0, '' , $reg[0])

next
Edited by iamtheky

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

Link to comment
Share on other sites

@iamtheky

Please, don't use _FileCountLines followed with a sequence of (open, skip ($i-1) lines, read line, close). It's about the worst way to do it. Re-read help file about FileReadLine.

Typical text files will fit in memory of typical stock PC under typical load as an "array of strings" form, so use _FileReadToArray, then process the array.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

It's about the worst way to do it.

makes want to start to a thread devoted to just that.....there has to be plenty of worse ways.

Other than not using the handle and keeping the file open throughout the linereads, how many other good practices did I airball?

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

Link to comment
Share on other sites

We can afford a quick parenthesis about that here. _FileCountLines does what its name spells out: it read the whole file and counts line breaks. Then repeatedly using FileReadLine with a line number parameter N causes (open the file, read until n-1 line breaks found, read from there untile next line break or EOF, close the file), a highly inefficient operation when repeated over any average-sized file. Best is to _FileReadToArray, populating an array in the process in a tight loop, then process the array contents in any suitable way.

Edit:

to witt, what happens when using functions without care.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

nicely explained, thanks for the aside.

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

Link to comment
Share on other sites

It seems I'm still having problems. Each line of my text lines contains words and one number and I need just the number.

$file = FileOpen("test.txt", 0)

$string = FileReadLine($file, 1)
MsgBox(0,"Line 1", $string) ; this shows the whole line
$nLine1 = StringRegExp($string, "[\d.]+", 3) ; this shows just the number I want (127), but it is a string

How do I get the string 127 to convert to the number 127?

Edited by Merrik
Link to comment
Share on other sites

Try this:

#include <File.au3>
Local $aValues
_FileReadToArray("test.txt", $aValues)
For $i = 1 To $aValues[0]
    $aValues[$i] = Number(StringRegExpReplace($aValues[$i], "(\d+)", "$1"))
Next

; Now, $aValues is an array of numbers
; $aValues[0] holds the number of data entries, which start at $aValues[1]

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

OK I tried this. I added "MsgBox(0, "The number is: ", $aValues[1])".

The text file has one line for testing "This is my number: 127"

The message box show a 0.

#include <File.au3>
Local $aValues
_FileReadToArray("test.txt", $aValues)
For $i = 1 To $aValues[0]
    $aValues[$i] = Number(StringRegExpReplace($aValues[$i], "(\d+)", "$1"))
Next

; Now, $aValues is an array of numbers
; $aValues[0] holds the number of data entries, which start at $aValues[1]

MsgBox(0, "The number is: ", $aValues[1])
Link to comment
Share on other sites

Oops, I beg your pardon. Was out of my head at this moment (but is that only at this moment?)...

#include <File.au3>
Local $aValues
_FileReadToArray("test.txt", $aValues)
For $i = 1 To $aValues[0]
    $aValues[$i] = Number(StringRegExpReplace($aValues[$i], "\D+", ""))
    MsgBox(0, "The number is: ", $aValues[$i])
Next
; Now, $aValues is an array of numbers
; $aValues[0] holds the number of data entries, which start at $aValues[1]

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

I thought I had answered this before and I was correct. Why didn't you keep it in the same thread as you started previously?

Here is a modified version of the code provided by jchd. The last part of the expression is because I seem to recall that you only wanted the first two decimal places

#include <File.au3>
Local $aValues
_FileReadToArray("test.txt", $aValues)
For $i = 1 To $aValues[0]
    $aValues[$i] = Number(StringRegExpReplace($aValues[$i], "\D*([\d]+\.?\d{0,2}).*", "$1"))
    MsgBox(0, "The number is: ", $aValues[$i])
Next

If what you wanted was really the value rounded to 2 decimal places then here is another demo script. I'm sure you can figure out how to use it in the loop of the first example.

$sStr = "There have been an average of 10.237 users per minute."
$sStr = Round(Number(StringRegExpReplace($sStr, "\D*([\d.]+).*", "$1")), 2)

MsgBox(0, "Result", $sStr)

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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...