Cusem Posted April 10, 2008 Share Posted April 10, 2008 This maybe is a stupid question, but I'm trying to turn a negative number (like -3, -0.15, -18) into a positive value (like 3, 0.15, 18). For instance: $var[5][2] = [[0.18, "Johnny"][-6, "Elsa"][2, "Robby"][0.57, "Esther"][-0.29, "Marcus"]] _Arraysort($var, 0, 0, 0, 2) I want extract the name that's closest to 0, however if I sort in this order, "Elsa" will be $var[0][0] and I want "Johnny" to become $var[0][0]. This is why I want to make the negative values positive ("elsa" and "marcus"), but I'm kinda stuck on how to do this the simplest way. Link to comment Share on other sites More sharing options...
Paulie Posted April 10, 2008 Share Posted April 10, 2008 (edited) You are looking for Abs() (Absolute Value) $Negative = Random(-100, -1,1) $Positive = Abs($Negative) Msgbox(0,"", "Negative: "&$Negative&@CRLF&"Positive: "&$Positive) Edited April 10, 2008 by Paulie Link to comment Share on other sites More sharing options...
Richard Robertson Posted April 10, 2008 Share Posted April 10, 2008 Paul, what do you think you're doing, answering my easy questions. Link to comment Share on other sites More sharing options...
MrCreatoR Posted April 10, 2008 Share Posted April 10, 2008 The same without using the Abs().. $Negative = Random(-100, -1,1) $Positive = -$Negative Msgbox(0,"", "Negative: "&$Negative&@CRLF&"Positive: "&$Positive)  Spoiler Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1  AutoIt Russian Community My Work... Spoiler Projects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize ProgramUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF Examples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating ) * === My topics === * ================================================== ==================================================    AutoIt is simple, subtle, elegant. © AutoIt Team Link to comment Share on other sites More sharing options...
weaponx Posted April 10, 2008 Share Posted April 10, 2008 The same without using the Abs().. $Negative = Random(-100, -1,1) $Positive = -$Negative Msgbox(0,"", "Negative: "&$Negative&@CRLF&"Positive: "&$Positive) This is NOT the same as abs(). Link to comment Share on other sites More sharing options...
amokoura Posted April 11, 2008 Share Posted April 11, 2008 if $number < 0 then $numberDoubleNegative = 2 * $number $numberPositified = $number - $numberDoubleNegative $number = $numberPositified endif Whoa I just won AutoIt Link to comment Share on other sites More sharing options...
Squirrely1 Posted April 11, 2008 Share Posted April 11, 2008 (edited) Cusem - this works to do what you want: #Include <Array.au3> ;Dim $var[5][2] = [["0.18", "Johnny"], ["-6", "Elsa"], ["2", "Robby"], ["0.57", "Esther"], ["-0.29", "Marcus"]] Dim $var[5][2] = [[0.18, "Johnny"], [-6, "Elsa"], [2, "Robby"], [0.57, "Esther"], [-0.29, "Marcus"]] For $i = 0 To UBound($var) - 1 ;$var[$i][0] = String(Abs(Number($var[$i][0]))) $var[$i][0] = Abs($var[$i][0]) Next _Arraysort($var, 0, 0, 0, 2) _ArrayDisplay($var) I'm not sure of the wisdom of mixing strings with numbers in an array, but this code seems to work. I initially changed the numbers in you defined array into strings and used the rem'd - out line to "ABS" the data. Here is from the help file under AutoIt > Language Reference > Variables: An Array is a variable containing series of data elements of the same type and size.... It was said that an Array contains only one datatype of the same type. But technically speaking, a Variant in AutoIt can contain anything from a number to a boolean value. So an AutoIt-Array could also contain different types, even other Arrays: ... This has not been strictly forbidden in AutoIt. However, it is NOT ADVISABLE to mix different datatypes in an Array. Especially the use of an Array inside another Array will severely affect the execution speed of your script. Edited April 11, 2008 by Squirrely1 Das Häschen benutzt Radar Link to comment Share on other sites More sharing options...
MrCreatoR Posted April 11, 2008 Share Posted April 11, 2008 This is NOT the same as abs().I didn't say that it is I only say that you not have to use Abs() to get positive number.  Spoiler Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1  AutoIt Russian Community My Work... Spoiler Projects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize ProgramUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF Examples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating ) * === My topics === * ================================================== ==================================================    AutoIt is simple, subtle, elegant. © AutoIt Team Link to comment Share on other sites More sharing options...
Cusem Posted April 11, 2008 Author Share Posted April 11, 2008 Thanks guys! This was exactly what I was looking for! Link to comment Share on other sites More sharing options...
Xenobiologist Posted April 11, 2008 Share Posted April 11, 2008 HI, can also do a * -1 Mega Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times Link to comment Share on other sites More sharing options...
amokoura Posted April 11, 2008 Share Posted April 11, 2008 Or: sqrt($num ^ 2) Link to comment Share on other sites More sharing options...
Richard Robertson Posted April 11, 2008 Share Posted April 11, 2008 That's probably the fastest outside of Abs. But then you will lose data if the number is large. Link to comment Share on other sites More sharing options...
Malkey Posted April 22, 2008 Share Posted April 22, 2008 Another Abs() equivalent $num * ((-1)^($num<0)) ,where $num is positive or negative. Only took 11days to think of it. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now