FuryCell Posted July 5, 2005 Posted July 5, 2005 I am trying to rotate a number. I wrote a UDF for it but it won't work properly all the time. This should return 5 but it returns 6 $Number=5 $Places=10 $Min=1 $Max=10 ClipPut( _NumberRotate($Number,$Places,1,$Min,$Max) ) Func _NumberRotate($iNumber,$iPlaces,$iDir,$iMin,$iMax) Local $iReturn,$iStep,$iCounter Select Case $iDir=1 For $iCounter=1 to $iPlaces step 1 $iReturn=$iNumber+1 If $iReturn>$iMax then $iReturn=$iMin If $iReturn<$iMin then $iReturn=$iMax Next Case $iDir=-1 For $iCounter=1 to $iPlaces step 1 $iReturn=$iNumber-1 If $iReturn>$iMax then $iReturn=$iMin If $iReturn<$iMin then $iReturn=$iMax Next EndSelect Return $iReturn EndFunc This should return 1 and does. $Number=2 $Places=3 $Min=1 $Max=2 ClipPut( _NumberRotate($Number,$Places,1,$Min,$Max) ) Func _NumberRotate($iNumber,$iPlaces,$iDir,$iMin,$iMax) Local $iReturn,$iStep,$iCounter Select Case $iDir=1 For $iCounter=1 to $iPlaces step 1 $iReturn=$iNumber+1 If $iReturn>$iMax then $iReturn=$iMin If $iReturn<$iMin then $iReturn=$iMax Next Case $iDir=-1 For $iCounter=1 to $iPlaces step 1 $iReturn=$iNumber-1 If $iReturn>$iMax then $iReturn=$iMin If $iReturn<$iMin then $iReturn=$iMax Next EndSelect Return $iReturn EndFunc How come it works with some numbers but not others. thanks in advance. HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.
LxP Posted July 5, 2005 Posted July 5, 2005 (edited) I am trying to rotate a number. I wrote a UDF for it but it won't work properly all the time.Without being 100% sure of what you're trying to achieve, is this what you're after?; deprecated - please see my next postAlternatively you could do away with the $iDir variable by simply specifying $iPlaces as a negative number to imply negative rotation, and then this code should suffice:; deprecated - please see my next post Edited July 11, 2005 by LxP
GaryFrost Posted July 5, 2005 Posted July 5, 2005 Func _NumberRotate($iNumber, $iPlaces, $iDir, $iMin, $iMax) Local $iReturn = $iNumber Local $iStep, $iCounter Select Case $iDir = 1 For $iCounter = 1 To $iPlaces Step 1 $iReturn = $iReturn + 1 If $iReturn > $iMax Then $iReturn = $iMin If $iReturn < $iMin Then $iReturn = $iMax Next Case $iDir = -1 For $iCounter = 1 To $iPlaces Step 1 $iReturn = $iReturn - 1 If $iReturn > $iMax Then $iReturn = $iMin If $iReturn < $iMin Then $iReturn = $iMax Next EndSelect Return $iReturn EndFunc ;==>_NumberRotate SciTE for AutoItDirections for Submitting Standard UDFs Don't argue with an idiot; people watching may not be able to tell the difference.
blindwig Posted July 5, 2005 Posted July 5, 2005 Why not make life a little easier and combine $iPlaces and $iDir into 1 variable? Func _NumberRotate($iNumber, $iAmount, $iMin, $iMax) Local $iRange = $iMax - $iMin + 1 $iNumber = $iNumber + $iAmount While $iNumber > $iMax $iNumber = $iNumber - $iRange WEnd While $iNumber < $iMin $iNumber = $iNumber + $iRange WEnd Return $iNumber EndFunc My UDF Threads:Pseudo-Hash: Binary Trees, Flat TablesFiles: Filter by Attribute, Tree List, Recursive Find, Recursive Folders Size, exported to XMLArrays: Nested, Pull Common Elements, Display 2dSystem: Expand Environment Strings, List Drives, List USB DrivesMisc: Multi-Layer Progress Bars, Binary FlagsStrings: Find Char(s) in String, Find String in SetOther UDF Threads I Participated:Base64 Conversions
FuryCell Posted July 5, 2005 Author Posted July 5, 2005 Thanks all. I am still not sure which one to use.I will run a quick check to make sure they all return the same answers first and then see which one is faster i will go with the faster one as i will be rotating many numbers as part of my simple encryption routine. -SolidSnake HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.
LxP Posted July 6, 2005 Posted July 6, 2005 Coding at 3AM is not best practice! Here's that code of mine again in a form that will work for $iMin <> 1:func _numberRotate($iNumber, $iPlaces, $iMin, $iMax) local $range = $iMax - $iMin + 1 return mod($iNumber - $iMin + $iPlaces + 1, $range) + $iMin - 1 endFuncOr if you still want to use $iDir:func _numberRotate($iNumber, $iPlaces, $iDir, $iMin, $iMax) if ($iDir < 0) then $iPlaces = 0 - $iPlaces local $range = $iMax - $iMin + 1 return mod($iNumber - $iMin + $iPlaces + 1, $range) + $iMin - 1 endFuncRegards,Alex
MSLx Fanboy Posted July 6, 2005 Posted July 6, 2005 Maybe using Number() to get the numeric value if string is a number Writing AutoIt scripts since _DateAdd("d", -2, _NowCalcDate())
FuryCell Posted July 6, 2005 Author Posted July 6, 2005 Coding at 3AM is not best practice! Here's that code of mine again in a form that will work for $iMin <> 1:func _numberRotate($iNumber, $iPlaces, $iMin, $iMax) local $range = $iMax - $iMin + 1 return mod($iNumber - $iMin + $iPlaces + 1, $range) + $iMin - 1 endFuncOr if you still want to use $iDir:func _numberRotate($iNumber, $iPlaces, $iDir, $iMin, $iMax) if ($iDir < 0) then $iPlaces = 0 - $iPlaces local $range = $iMax - $iMin + 1 return mod($iNumber - $iMin + $iPlaces + 1, $range) + $iMin - 1 endFuncRegards,Alex<{POST_SNAPBACK}>thank you. I do a lot of my coding late at night too. It is nice and Quiet then. LOL. HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.
FuryCell Posted July 10, 2005 Author Posted July 10, 2005 I have made up my mind and decied to use the one BlindWig modified from GaFrosts post. This is becuase I seem to be having problems with the one LxP made. Thanks All. -SolidSnake HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.
LxP Posted July 11, 2005 Posted July 11, 2005 I have made up my mind and decied to use the one BlindWig modified from GaFrosts post. This is becuase I seem to be having problems with the one LxP made.Just so I can figure out where I went wrong and if you don't mind spending the time, could you please post a couple examples where my code didn't work for you? I certainly don't want to be sharing bad code!
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