View Full Version : length property
oluc
September 30th, 2009, 07:22 PM
This is probably a stupid question needing a simple answer:
I am looking at "length" property in the Adobe AS3.0 language ref as I do not understand it.
It says:
An integer specifying the number of characters in the specified String object. (I understand this)
Because all string indexes are zero-based, the index of the last character for any string x is x. length - 1. (I do not understand the "- 1". I understand that all string indexes are zero-based but I cant put this together with the last character for any string being -1 or whatever it is saying.
Do you understand this and can you help me understand it?
Thanks in advance.
senocular
September 30th, 2009, 07:37 PM
consider a string:
"a"
Do you know in what index the 'a' character exists? 0 - because its 0-based. Now, what is the length of the string? 1 - because there is one character. The one character, 'a', is the last character meaning 0 is the last index. The last index is the length (1) - 1 ( = 0).
"abc"
c is at index 2, the length is 3, 3 - 1 = 2 which is the index of the last character.
That's what it means.
oluc
September 30th, 2009, 07:50 PM
Thank you, thank you, thank you!
oluc
September 30th, 2009, 08:00 PM
I have another one:
what is "?" as in:
imgNum = imgNum < lastImageIndex ? imgNum + 1 : 0;
and how does the "1 : 0" work?
this is code on a next button that makes the next image of an xml come on screen.
some of the prior code before btn code:
var imgData:XML;
var imgNum:int = 0;
var lastImageIndex:int = 0;
rawImage = imgData.image[imgNum].imgURL;
lastImageIndex = imgData.*.length() - 1;
_kp
September 30th, 2009, 08:32 PM
It's called Ternary operator, used in place of a simple if/else statement, especially for null checks (http://opensource.adobe.com/wiki/display/flexsdk/Coding+Conventions)
It checks the term before the ? and then uses the term before the colon if true or the term after the colon if false.
imgNum = imgNum < lastImageIndex ? imgNum + 1 : 0; does the same as
if(imgNum < lastImageIndex) imgNum = imgNum + 1;
else imgNum = 0;
oluc
October 1st, 2009, 01:22 PM
Thanks!
So If I have the following code on a "Next Image" button:
imgNum = imgNum < lastImageIndex ? imgNum + 1 : 0;
How do I set it up so that it goes backwards and brings up the previously displayed pic?
imgNum = imgNum < lastImageIndex ? imgNum - 1 : (not sure what to put in place of 0);
_kp
October 1st, 2009, 01:52 PM
The code checks if the imgNum doesn't exceed the range of an index. If you want to go backwards you did the right thing to decrease imgNum. But in this case you don't need to check if it higher than your last lastImageIndex but if it's lower than 1, because 0 is is the first index element an decreasing it will cause errors.
imgNum = imgNum > 1 ? imgNum-1 : lastImageIndex If imgNum is 0 and the button is clicked, it simply jumps to the last image.
oluc
October 2nd, 2009, 08:19 PM
Thanks!
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.