Jump to content

Issue with Number function


Go to solution Solved by jchd,

Recommended Posts

Hi everybody in the community.

I'm new to AutoIt and trying to make my second script (the first one was succesful).

I use a custom view in Total Commander to get some torrent files data, like the size of the torrent content (see the image below). My goal is to get a msgbox that gives me the total size of the selected torrents content (TC status line just gives the total size of the torrents files).

Using an internal TC command I export the custom details, and after some formatting I get the string in the needed format for making addition (xxx.x+xx.x+xxxx.x+xxx.x).

The issue is that when i use Number to retrieve the total I just get the first part of the string, before the first "+" (in the example, I get "778.2").

Just for testing if the string was correct, I put it into clipboard and paste it directly in the Number function and that way I get the total (in the example, "4914.5").

I couldn't been able to find an answer for this in the forum, and I really don't understand it.

os5u.jpg

Here's the complete code.

#include "SendMessage.au3"; se necesita esta línea por la función _SendMessage
WinActivate("Total Commander"); activa la ventana de TC
$tcch=WinGetHandle("Total Commander"); guarda en una variable el control handle de TC para la función _SendMessage
_SendMessage($tcch,0x433,2036,0); ejecuta cm_CopyFileDetailsToClip
$clip0=StringRegExpReplace(ClipGet(),"(.+\.torrent\t)(.+)( Mb.+)","$2")
;~ MsgBox(262144,'line ~' & @ScriptLineNumber-1,'$clip0 devuelve:' & @lf & @lf & $clip0); ### Debug MSGBOX
$clip1=StringRegExpReplace($clip0,"\v","+")
;~ MsgBox(262144,'line ~' & @ScriptLineNumber-1,'$clip1 devuelve:' & @lf & @lf & $clip1); ### Debug MSGBOX
$clip2=StringRegExpReplace($clip1,"\.","")
;~ MsgBox(262144,'line ~' & @ScriptLineNumber-1,'$clip2 devuelve:' & @lf & @lf & $clip2); ### Debug MSGBOX
$clip3=StringRegExpReplace($clip2,",","\.")
;~ MsgBox(262144,'line ~' & @ScriptLineNumber-1,'$clip3 devuelve:' & @lf & @lf & $clip3); ### Debug MSGBOX
ClipPut($clip3)
;~ $total=Number($clip3)
$total=Number(778.2+89.0+3567.7+479.6)
MsgBox(0,"Peso total de los torrents seleccionados",$total)

Thanks in advance.

Link to comment
Share on other sites

You are expecting that a string containing for instance "778.2+89.0+3567.7+479.6" will evaluate to the total of numbers added in the string. That won't happen by itself.

Try using Execute:

$total=Execute("778.2+89.0+3567.7+479.6")

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

Am I the only one here that thinks this is against the forum rules ?


Time you enjoyed wasting is not wasted time ......T.S. Elliot
Suspense is worse than disappointment................Robert Burns
God help the man who won't help himself, because no-one else will...........My Grandmother

Link to comment
Share on other sites

  • Moderators

BigDod,

Which rule do you feel it infringes? :huh:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

Downloading from a Torrent site

Edit - If I am wrong I apologize for wasting peoples time. 

Edited by BigDod


Time you enjoyed wasting is not wasted time ......T.S. Elliot
Suspense is worse than disappointment................Robert Burns
God help the man who won't help himself, because no-one else will...........My Grandmother

Link to comment
Share on other sites

  • Moderators

BigDod,

But the OP's question is how to total figures read from a Total Commander GUI - not about downloading from a torrent site. As such I believe that the thread as posted is not against the rules. And please do not apologise for asking - better safe than sorry. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

Many thanks for all the answers. Incredibly fast assistance.

@jchd & @JohnOne

The Execute function did what I wanted. Thanks.

But according to Number function description, ¿wouldn't it do the same thing in this case?

The output of the StringRegExpReplace functions are as follows:

i2cq.jpg ocmq.jpgm9ho.jpg kn12.jpg

And here is the corrected code that does the trick.

#include "SendMessage.au3"; se necesita esta línea por la función _SendMessage
WinActivate("Total Commander"); activa la ventana de TC
$tcch=WinGetHandle("Total Commander"); guarda en una variable el control handle de TC para la función _SendMessage
_SendMessage($tcch,0x433,2036,0); ejecuta cm_CopyFileDetailsToClip
$clip0=StringRegExpReplace(ClipGet(),"(.+\.torrent\t)(.+)( Mb.+)","$2")
;~ MsgBox(262144,'line ~' & @ScriptLineNumber-1,'$clip0 devuelve:' & @lf & @lf & $clip0); ### Debug MSGBOX
$clip1=StringRegExpReplace($clip0,"\v","+")
;~ MsgBox(262144,'line ~' & @ScriptLineNumber-1,'$clip1 devuelve:' & @lf & @lf & $clip1); ### Debug MSGBOX
$clip2=StringRegExpReplace($clip1,"\.","")
;~ MsgBox(262144,'line ~' & @ScriptLineNumber-1,'$clip2 devuelve:' & @lf & @lf & $clip2); ### Debug MSGBOX
$clip3=StringRegExpReplace($clip2,",","\.")
;~ MsgBox(262144,'line ~' & @ScriptLineNumber-1,'$clip3 devuelve:' & @lf & @lf & $clip3); ### Debug MSGBOX
;~ ClipPut($clip3)
$total=Execute($clip3)
;~ $total=Number(778.2+89.0+3567.7+479.6)
MsgBox(0,"Peso total de los torrents seleccionados",$total)

Thanks again. I feel I'm learning a lot with this wonderful software.

Link to comment
Share on other sites

  • Solution

Also guys, don't forget that torrents are widely used for perfectly legal operations, like spreading large files like Linux distros, countless nightly builds, a.s.o.

No STASI-like reporting, please!

@facinlaburo,

Output of your Stringreplace is a string, (hint: not an AutoIt statement!). Number() can convert the string representation of a number (e.g. Number("123.5") --> 123.5) but is not the right tool to evaluate a string which contains the right-handed part of a statement, like "123 + 456 *3 - 57.8".

See the difference?

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

@facinlaburo,

Output of your Stringreplace is a string, (hint: not an AutoIt statement!). Number() can convert the string representation of a number (e.g. Number("123.5") --> 123.5) but is not the right tool to evaluate a string which contains the right-handed part of a statement, like "123 + 456 *3 - 57.8".

See the difference?

 

What confuses me is that if I directly input "123+456*3-57.8" on Number() I get "1433.2" but if I retrieve "123+456*3-57.8" from clipboard and pass it to Number() I get "123". I'm afraid my answer to your question is "No, I don't" although I would like to. Anyway, from now on I'll use Execute() for similar situations.

Link to comment
Share on other sites

Well you must be using a different AutoIt than I, because I cannot get what you say you do.

 

Try this code and look at what is written in each *.tmp file.

$test1=number(123+456*3-57.8)
FileOpen("number()test1.tmp",2)
FileWrite("number()test1.tmp",$test1)
$test=ClipPut("123+456*3-57.8")
$test2=Number(ClipGet())
FileOpen("number()test2.tmp",2)
FileWrite("number()test2.tmp",$test2)

I'm using AutoIt 3.3.8.1 zip version.

Link to comment
Share on other sites

I don't need to write files I know what will be written, It's all documented right there in the help file.

It's exactly the same as this.

$test1=number(123+456*3-57.8)
$test=Number("123+456*3-57.8")
ConsoleWrite($test1 & @LF); 1433.2
ConsoleWrite($test & @LF); 123

One is a string (same as you get from ClipGet() the other is not

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Expanding on the same info JohnOne said.

See remarks and example of Number function in help file and comments in this example.

ConsoleWrite(Number(123 + 456 * 3 - 57.8) & @LF) ; Returns 1433.2
ConsoleWrite(123 + 456 * 3 - 57.8 & @LF)         ; Returns 1433.2

ConsoleWrite(Number("123+456*3-57.8") & @LF) ; Returns 123 from string (From left to right the
; first digits only are 123. The rest of the string is stripped). See Number function in help file.

ClipPut("123+456*3-57.8")
ConsoleWrite(Number(ClipGet()) & @LF) ; Returns 123 being the first occurring numbers from the string.

AutoIt Script Editor is a must have. One really small benefit is:-
Instead of writing output to a file an easier method of displaying results is using the SciTE text editor, and using the ConsoleWrite function in the AutoIt script.

ConsoleWrite function used within SciTE outputs to the bottom window of the SciTE text editor when the ".au3" script is run. Pressing F8 button in SciTE toggles the output pane on and off.

Link to comment
Share on other sites

@JohnOne & @Malkey

Thanks so much for your advice. ConsoleWrite() is really very useful and better than MsgBox() or writing to a file.

After making some tests, I realized my mistake was to think that information retrieved from clipboard could be treated as numeric and it can't. ¿Is that correct?

$test1=Number(123+456*3-57.8)
$test2=Number("123+456*3-57.8")
ConsoleWrite($test1 & @LF); 1433.2
ConsoleWrite($test2 & @LF); 123
ClipPut(123+456*3-57.8)
ConsoleWrite(ClipGet() & @LF); 1433.2
ClipPut("123+456*3-57.8")
ConsoleWrite(ClipGet() & @LF); 123+456*3-57.8
$test3=ClipGet()
ConsoleWrite($test3 & @LF); 123+456*3-57.8
ConsoleWrite(Number($test3) & @LF); 123
ConsoleWrite(IsNumber($test3) & @LF); 0
ConsoleWrite(IsNumber(123+456*3-57.8) & @LF); 1
ClipPut(1243)
$test4=ClipGet()
ConsoleWrite($test4 & @LF); 1243
ConsoleWrite(IsNumber($test4) & @LF); 0
ConsoleWrite(IsNumber(1243) & @LF); 1

Many thanks to all of you for your kind consideration with such an ignorant like me.

I suppose I'll bother you all with other posts to continue learning.

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