I have RTRIM; how to make LTRIM with regexp - actionscript-3

I have RTRIM; how to make LTRIM one?
public static function rtrim(string:String):String
{
return string.replace(/\s+$/,"");
}

public static function ltrim(string:String):String {
return string.replace(/^\s+/,"");
}
Caveat: Untested! Look up the flex 3.0 documentation here. This is exactly similar to what you have, except that we use a different metacharacter to specify that we want to start searching for whitespaces (\s -- another metacharacter) from the begining(^) instead of from the end($). The + after \s tells the pattern matches to grok one or more whitespaces.

Instead of re-inventing the wheel, why not just use the StringUtil class from Adobe's as3corelib library?
Out of interest, as3corelib defines it's trim functions as follows:
public static function trim(input:String):String
{
return StringUtil.ltrim(StringUtil.rtrim(input));
}
public static function ltrim(input:String):String
{
var size:Number = input.length;
for(var i:Number = 0; i < size; i++)
{
if(input.charCodeAt(i) > 32)
{
return input.substring(i);
}
}
return "";
}
public static function rtrim(input:String):String
{
var size:Number = input.length;
for(var i:Number = size; i > 0; i--)
{
if(input.charCodeAt(i - 1) > 32)
{
return input.substring(0, i);
}
}
return "";
}

Wow, seriously? You're using regular expressions to remove a constant sequence of characters from the ends of a string? Really? I don't know Actionscript/Flex, but this isn't the way to go. After a quick google I found a solution which may or may not be more efficient.

Related

making function is number is odd or even

I want to make a function to find a number is odd or even by using only increment or decrement function . no another operator like equal or modular operator
def isOdd(n:Int) = {
if(n <= 1) n;
else isOdd(n - 2);
}
this will return 1 or 0 (true or false) whether or not the number n is odd.
I forgot to mention that this code is runnable in Scala.
It's tail-recursive, too.
check something like this with % mod operator
using System;
class Program
{
static void Main()
{
for (int i = 0; i <= 100; i++)
{
if (IsOdd(i))
{
Console.WriteLine(i);
}
}
}
public static bool IsOdd(int value)
{
return value % 2 != 0;
}
}
Maybe something like this? This solution however does use assignment and logical operators.
var isEven = true;
function makeDivayanshusHomework (number) {
if (number < 0) {
for(i = 0; i < 2*number; i++) {
number++;
}
}
while (number > 0) {
number--;
isEven = isEven ? false : true;
}
return isEven;
}
edit: as per sascha10000's comments, the solution below is perhaps even somewhat more functional:
function makeDivayanshusHomework (number) {
if (Math.sqrt(number*number) > 1) {
return makeDivayanshusHomework(number-2)
}
else {
return Math.sqrt(number*number); // 1 when odd, 0 otherwise
}
}
which brings me almost to sascha10000's original answer, although this method gracefully handles negative integers. Did your teacher specify to what extent the input has to be sanitised?
perhaps
public bool isEven(int number){
return number/2.0 == Convert.ToInt32(number/2.0)
}
I'm not quite sure how you intend to test a number for being even without an equals or mod operator.
Lucas

Nonfunctioning "for loop" for addressing each variable of class

I'm learning AS3 but have some antiquated background in programming (TP and Atari Basic). On this forum I learned to use a loop such as the one below to address each variable in an object class, in order to make a clone of the object (deep or shallow) or in my case to build the text for a tooltip. However mine doesn't work. Here's the loop, following is an explanation, any help you can give I'd appreciate greatly!
var tooltipText:String;
var i:String;
for (i in bsm) {
if (!(bsm[i] is String)) {
if (bsm[i] != 0) {
tooltipText = i + ": " + bsm[i];
tooltip.extendTooltip(tooltipText, 0xFFFFFF);
}
}
}
Please forgive the horrible variable names. 'i' is a String. 'bsm' is a non-null instance of class StatMod, which begins with
public class StatMod extends Object {
public static const ENCHANTMENTMODIFIER:String = "enchantmentModifier";
public var enchantmentType:String = "None";
public var enchantmentDescriptor:String = "None";
public var minDamage:Number = 0;
public var maxDamage:Number = 0;
public var attackSpeed:Number = 0.2;
The intended behavior is to go through each of the variables of StatMod (I'm not showing them all and I will add more later), and if the variable is a non-zero number, make a string ("attackSpeed: 0.2" for instance) and then add that string to the tooltip. The tooltip.extendTooltip function is working properly.
The observed behavior is basically the computer believing that there are no variables in bsm.
What can I say or do to convince the computer that there actually are variabels in bsm?
The behavior you're expecting is only the case when iterating over dynamically attached properties. For example, if you marked your class dynamic:
public dynamic class StatMod { }
Then added some values to it at runtime:
bsm.test = 5;
Your loop will find the property test with the value 5.
Some options you have to achieve what you want are:
Extend the Proxy class to define what properties are iterable via nextName and nextNameIndex.
Use describeType to generate a list of all the public properties.
Though a simpler method is to expose a list of the properties you want to iterate over and use that in your loop instead, something like:
public class StatMod {
// Existing properties etc.
private _properties:Vector.<String>;
public function get properties():Vector.<String> {
if (_properties === null) {
_properties = new <String>[
'enchantmentType',
'enchantmentDescription',
'minDamage',
'maxDamage',
'attackSpeed'
];
}
return _properties;
}
}
Then:
for (var i:int = 0; i < bsm.properties.length; i++) {
var prop:String = bsm.properties[i];
trace(prop, bsm[prop]);
}

Sorting an array by a private variable

So... I have spent the last hour trying to figure out why the sort method for my array was not working properly, when I realized that the variable I was trying to sort by in my objects was not publicly available. I have accessed it by using a getter method, which has worked fine for all other purposes. My questions is: Is it possible to sort by a private variable somehow? Perhaps by using a getter method, but I don't know how that would work syntactically. Or do I just have to make my variable public?
On a slightly related note, is there some way to sort on a variable of an object in a vector using standard methods?
Here is an example using get function to sort
var k:Vector.<A> = new Vector.<A>();
k.push(new A(5));
k.push(new A(3));
k.push(new A(7));
k.push(new A(1));
k.push(new A(9));
k.sort(compareFunction);
private function compareFunction(obj1:Object, obj2:Object, properties:Array = null):int
{
var a1:A = obj1 as A;
var a2:A = obj2 as A;
if (a1.level > a2.level)
{
return 1;
}
else if (a1.level < a2.level)
{
return -1;
}
else
{
return 0;
}
}
class A {
private var _level:int;
public function get level():int
{
return _level;
}
public function A($level:int)
{
_level = $level
}
}

How to instantiate an array of custom classes in Action Script 3.0

I'm new to AS3 and I'm getting this error while trying to implement OO style code.
Incorrect number of arguments. Expected no more than 0.
When I try to:
var countries:Country = new Country(10);
Normally this would work in Java or C++, so I'm not sure what's up!?
Here is my custom class.
package {
public class Country {
var cName:String = "noName";
public function Country() {
// constructor code
}
public function setName(n:String):void {
cName = n;
}
public function getName():String {
return cName;
}
}
}
You are passing 10 to the constructor, which is not what you want to do. To instantiate an array of instances, try something like this:
var countries:Array = []
var country:Country;
for (var i:uint = 0; i < 10; i++) {
country = new Country()
country.setName("Country_" + i);
countries.push(country)
}
your constructor function public function Country() {} not have an argument, but you give 10, must go wrong.
ActionScript's array not like c++, don't need element type <Country>
you want to save class in array is simple: var arr:Array = [new Country()]

AS3: Optimizing Object Memory Size

I have have a class that I wrote, and it seems bigger than it should be. It doesn't extend anything, and has very little going on - or so I thought - but each one is taking up just under 100k100 bytes ( thanks back2dos ). I guess that I don't have a very good understanding of what really affects how much memory an object takes up in AS3.
If anyone can point me to some reading on the subject that might be helpful, or perhaps explain some insight into how to think about this, that would be awesome.
I would like to keep a LOT of these objects in memory - and I thought I could until now, but at this size I'm going to have to create them or use an object pooling technique of some kind.
Thanks for the assistance.
Edit: Although I've got this in order, I'm keeping the code I posted here for completeness. The class has been heavily modified from the original version. Values that were referencing other files have been made static as to allow the code to run for someone else ( in theory hehehe... ).
Although my situation is sorted out, I'll give the answer to a good reference for information on classes and memory.
In this case the class has 15 variables. I'm only using a single String and a bunch of ints, Numbers, and Booleans with some references to more of the same in globally available XML data. It also imports Point for the constructor, though no points are stored. In testing, even without the global XML references or Point class it's still around a ~84k each. There are getters for 7 of the variables and a couple methods in addition to the constructor. All of which are less than 20 lines ( and I have a very sparse coding style ).
The class mentioned for reference, but feel free to generalize:
package
{
public class AObject
{
private var _counter:int;
private var _frames:int;
private var _speed:int;
private var _currentState:String;
private var _currentFrame:int;
private var _offset:int;
private var _endFrame:int;
private var _type:int;
private var _object:int;
private var _state:int;
private var _x:Number;
private var _y:Number;
private var _w:int;
private var _h:int;
private var _update:Boolean;
public function AObject( targetX : int, targetY : int, state : int, object : int, type : int )
{
_x = targetX;
_y = targetY;
_type = type;
_object = object;
_state = state;
_counter = 0;
_w = 32;
_h = 32
_update = true;
setState( state );
}
public function setState( state:int ) : void
{
_currentState = "bob";
var frameCounter : int = 0;
var stateCounter : int = state - 1;
while ( state > 0 )
{
frameCounter += 4;
--stateCounter;
}
_offset = frameCounter;
_currentFrame = _offset;
_speed = 10;
_frames = 4;
_endFrame = _offset + _frames - 1;
}
public function get state() : int
{
return _state;
}
public function animate() : Boolean
{
if ( count() )
{
if( _currentFrame < _endFrame )
{
++_currentFrame;
}
else
{
_currentFrame = _offset;
}
_speed = 10;
return true;
}
else
{
return false;
}
}
private var adder: Number = 0;
private function count():Boolean
{
_counter++;
if ( _counter == _speed )
{
_counter = 0;
return true;
}
else
{
return false;
}
}
public function get x():int
{
return _x;
}
public function get y():int
{
return _y;
}
public function get type():int
{
return _type;
}
public function get object():int
{
return _object;
}
public function get currentFrame():int
{
return _currentFrame;
}
public function get w():int
{
return _w;
}
public function get h():int
{
return _h;
}
}
}
i am amazed, this compiles at all ... when i try to compile it with the flex SDK, it creates an enormous collision with the built-in class Object, which is the base class of any class, making my trace output overflow ...
other than that, this is an infinite loop if you pass a value for state bigger than 0
while ( state > 0 )
{
frameCounter += 4;
--stateCounter;
}
but it seems really strange these objects are so big ... after renaming and taking care not to pass in 0 for the state, i ran a test:
package {
import flash.display.Sprite;
import flash.sampler.getSize;
import flash.system.System;
public class Main extends Sprite {
public function Main():void {
const count:int = 100000;
var start:uint = System.totalMemory;
var a:Array = [];
for (var i:int = 0; i < count; i++) {
a.push(new MyObject(1, 2, 0, 4, 5));
}
var mem:uint = System.totalMemory - start - getSize(a);
trace("total of "+mem+" B for "+count+" objects, aprox. avg. size per object: "+(mem/count));
}
}
}
it yields:
total of 10982744 B for 100000 objects, aprox. avg. size per object: 109.82744
so that's quite ok ... i think the actual size should be 4 (for the bool) + 4 * 11 (for the ints) + 4 (for the reference to the string) + 8 * 3 (for the three floats (you have the adder somewhere over the count) + 8 for an empty class (reference to the traits objects + something else), giving you a total of 88 bytes ... which is, what you get, if you getSize the object ... please note however, that getSize will only give you the size of the object itself (as calculated here) ignoring the size of what strings or other objects your object references ...
so yeah, apart from that name you definitely should change, the problem must be somewhere else ...
greetz
back2dos
If you really want to save on space, you can fake shorts by using unsigned integers, and using upper/lower bits for one thing or another.
ints are 4 bytes by nature, you can reuse that int on anything less than 2^8.
width height
0xFFFF + 0xFFFF
offset endframe
0xFFFF + 0xFFFF
This though gets ugly when you want to write anything or read anything, as to write width or height you'd have to:
writing:
size = (width & 0x0000FFFF) << 16 | (height & 0x0000FFFF);
reading:
get width():uint { return (size & 0xFFFF0000) >> 16 };
That's ugly. Since you're using getters anyways, and assuming computation speed is not an issue, you could use internal byte arrays which could give you even more granularity for how you want to store your information. Assuming your strings are more than 4 bytes, makes more sense to use a number rather than a string.
Also, I believe you will actually get some memory increase by declaring the class as final, as I believe final functions get placed into the traits object, rather than