View Full Version : find longest string / the easiest way
lope
July 2nd, 2009, 10:31 AM
I am calling this function:
function getHighestWidth():void{
for (var i:int = 0; i < myXML.video_title.length(); i++) {
trace(String(myXML.video_title[i]).length);
}
}
output gives like:
22
43
54
24
52
64
67
...
I could push these numbers into array and then sort that array numeric to get the highest number, but is there an even shorter way to return the highest number from these strings?
Dimonte
July 2nd, 2009, 10:38 AM
function getMaxLength():int{
var maxLength:int = 0;
for (var i:int = 0; i < myXML.video_title.length(); i++) {
maxLength = Math.max(String(myXML.video_title[i]).length, maxLength);
}
return maxLength;
}
irrationalistic
July 2nd, 2009, 10:41 AM
Before you start your loop add a variable called maxWidth set to 0 and inside your loop, check to see if the current width is greater than maxWidth.
function getHighestWidth():int{
var maxWidth = 0;
for (var i:int = 0; i < myXML.video_title.length(); i++) {
trace(String(myXML.video_title[i]).length);
if(String(myXML.video_title[i]).length > maxWidth){
maxWidth = String(myXML.video_title[i]).length;
}
}
return maxWidth;
}
that will return the length of the longest string!
lope
July 2nd, 2009, 10:41 AM
you know what, disregard this, I actually need highest textfield width, string length doesnt do me good because letters are different width...
currently, I am using this code, so is there an shorter/easier way?
var highestWidth:uint;
function getHighestWidth():void{
var temp:Array = new Array();
for (var i:int = 0; i < myXML.video_title.length(); i++) {
var playlist_item:Playlist_item = new Playlist_item();
playlist_item.playlist_item_txt.autoSize = TextFieldAutoSize.LEFT;
playlist_item.playlist_item_txt.text = myXML.video_title[i];
temp.push(playlist_item.playlist_item_txt.width);
}
temp.sort(Array.NUMERIC);
highestWidth = temp[temp.length - 1];
trace(highestWidth);
}
Dimonte
July 2nd, 2009, 10:47 AM
Well, apart from replacing the whole array sorting thing with a single maxWidth variable and using Math.max for comparison, adding text to autosized text field seems to be the way to do it.
HotN
July 2nd, 2009, 10:50 AM
You can do the same thing with the width that was being suggested for character count. start with a variable set to 0. Each time you run through your loop, compare the text field width to that variable. If the width is greater, reset the var to the new width.
Doing that avoids the need to create and sort arrays.
lope
July 2nd, 2009, 11:23 AM
You can do the same thing with the width that was being suggested for character count. start with a variable set to 0. Each time you run through your loop, compare the text field width to that variable. If the width is greater, reset the var to the new width.
Doing that avoids the need to create and sort arrays.
good idea, thanks!
lope
July 2nd, 2009, 11:27 AM
Well, apart from replacing the whole array sorting thing with a single maxWidth variable and using Math.max for comparison, adding text to autosized text field seems to be the way to do it.
I am asking this because I have to make first pass through the loop the get the maxWidth and second pass to actually set something on each movieClip to maxWidth, and I dont like two passes :P
Dimonte
July 2nd, 2009, 11:45 AM
Well, it is the two-pass operation. You can't start setting something when the value isn't known yet, can you? :)
bLasTamos
July 2nd, 2009, 12:03 PM
Ok there's something I'm not getting here lope.
Why do you need a sorted array when you only need the highest value?
Also, do you think by sorting the array you are not going through it again?
Just do what Dimonte says...
lope
July 2nd, 2009, 02:59 PM
Ok there's something I'm not getting here lope.
Why do you need a sorted array when you only need the highest value?
Also, do you think by sorting the array you are not going through it again?
Just do what Dimonte says...
no, I dont use array, I do like HotN said;
but I still need 2 loops through my xml, first for highest value and second for actually using this value
theCodeBot
July 2nd, 2009, 03:28 PM
Why don't you just use the Math.max function?
var lengths:Array = [];
for(var i:int=0;i<someLength;i++) {
lengths.push(lengthYouGather);
}
var maxLength = (Math.max as Function).apply(this,lengths);
//Look up Math.max's returns
//And Function.apply if you don't understand this.
bLasTamos
July 2nd, 2009, 03:49 PM
Is there a reason to call the max function with all n values as arguments instead of calling it n times? I can't think of any algorithm lower than O(n) for that.
Or maybe you are just considering the function calling overhead... but if you're going that way then I say you are wasting more time and memory using an array to push your values.
lope
July 2nd, 2009, 04:36 PM
Why don't you just use the Math.max function?
ActionScript Code:
var lengths:Array = [];
for(var i:int=0;i<someLength;i++) {
lengths.push(lengthYouGather);
}
var maxLength = (Math.max as Function).apply(this,lengths);
//Look up Math.max's returns
//And Function.apply if you don't understand this.
string length doesnt help me, I need textfield width!
bLasTamos
July 2nd, 2009, 08:12 PM
That's not for the string length, thats a generic algorithm for finding the max value. You will obviously have to adapt it to your code...
lope
July 3rd, 2009, 05:08 AM
That's not for the string length, thats a generic algorithm for finding the max value. You will obviously have to adapt it to your code...
yes I know, I did adopt it for that, thanks!
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.