A Flash Developer Resource Site

Results 1 to 16 of 16

Thread: Variable problem in embedded loop

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    15

    Variable problem in embedded loop

    I'm reposting this question because I made a newbie mistake to my first post - then I couldn't edit it, blah blah blah...

    So...

    I'm wanting to convert a string to an array of ASCII equivalents. I can do this fine, but I want it so if the string is LESS than 150 characters, I want the conversion from a string to ASCII to loop until there are 150 characters.

    I could use an MD5 function but that's quite complex (for me) to implement is AS3.

    Here's my function:

    Code:
    function stringToAscii (someString) {
    	for (var loop=1; loop <= 150; loop++) { // loop up to 150 times
    		var arry = someString.split("");
    		for (i in arry) {
    			arry[i] = arry[i].charCodeAt(0);
    			loop++; // increase the loop number because we want a TOTAL of 150 numbers
    		}
    	};
        return arry;
    }
    I call the function with: stringToAscii("bob smith");

    "bob smith" is 9 characters and it creates an array of 9 numbers. The problem is it doesn't seem to keep looping until there's 150 numbers in the array.

    Any help would be appreciated

  2. #2
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    is this method at all helpful -
    PHP Code:
    function stringToAscii (someString) {
    arry someString.split("");        
    for (
    i=0;i!=arry.length;i++) {
    arry[i] = arry[i].charCodeAt(0);
    }
    for (var 
    j=i;j!=150;j++) {
    arry[j] = j// or - random(j)
    }
    trace(arry.length);
    return 
    arry;
    };


    trace(stringToAscii("bob smith")); 

  3. #3
    Junior Member
    Join Date
    Jun 2009
    Posts
    15

    Kinda...

    Thanks for the help but it's not quite what I need.

    I need a 150 character string that is the same every time "bob smith" is entered, just like an MD5 hash. So, I guess I'm after a really simply hash on a string.

    It appears that the line:
    var arry = someString.split("");

    which defines the variable, 'arry' is not accessible from within the nested loop. The script seems to consider the variable empty if that line is NOT immediately after the first loop.

    If there's a better way to achieve what I'm after then I'm up for it, otherwise I need to work out why this embedded loop isn't working.

    Adam

  4. #4
    newb of many sorts Ralgoth's Avatar
    Join Date
    Apr 2002
    Posts
    466
    I'm pretty sure that mod dog's script does just that...

    Code:
    var array1:Array = stringToAscii("bob smith");
    var array2:Array = stringToAscii("bob smith");
    
    trace(array1.length);
    // returns 150
    
    trace(array1.toString() == array2.toString());
    // returns true
    however, it's returning an array with a length of 150. As a string length it would vary.

    that aside, is there any reason you can't use a server side script for this?
    Last edited by Ralgoth; 06-23-2009 at 08:02 AM.
    Search first, asked questions later.

  5. #5
    Junior Member
    Join Date
    Jun 2009
    Posts
    15

    Not exactly

    mod dog's script creates an array with the ASCII codes for "bob smith" but then just number from then on, incrementing by 1.

    I want 150 characters, not numbers.

    For example, I want "bob smith" to result in, at least:

    "bob smithbob smithbob smithbob smith...." up to 150 characters of which I then get the ASCII codes for. I can then add in some maths to mix it up a bit, but just so long as the ASCII string for "bob smith" is the SAME each time. So random maths can't be used.

    The best analogy is that I want to create a very basic cypher like an MD5 hash but without all the complexity of doing a real MD5 hash. The result MUST be 150 numbers, comma separated (or in an array). The easiest way I know to get 150 number from a short string is to use ASCII codes and build from there.

    Adam
    Last edited by geniusprinting; 06-23-2009 at 08:04 AM. Reason: more info

  6. #6
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    if you are considering the MD5 hash option -
    http://board.flashkit.com/board/showthread.php?t=761778

  7. #7
    Junior Member
    Join Date
    Jun 2009
    Posts
    15
    Yeh thanks for that. I started there and tried to implement as described but I'm new to Flash so bit of a steep learning curve and also overkill for what I need.

    I'm really at a loss as to why my original code doesn't work. I haven't tried it in PHP but I'm guessing if I did it'd work. I really feel like the variable arry is being lost somehow in the looping.

  8. #8
    newb of many sorts Ralgoth's Avatar
    Join Date
    Apr 2002
    Posts
    466
    So why not convert the numbers to characters?

    Code:
    function stringToAscii(someString):String
    {
    	var arry:Array = someString.split("");
    	for (var i=0; i!=arry.length; i++) {
    		arry[i] = arry[i].charCodeAt(0);
    	}
    	for (var j=i; j!=150; j++) {
    		arry[j] = j;// or - random(j)
    	}
    	return numsToChars(arry);
    }
    
    function numsToChars(a:Array):String
    {
    	var s:String = '';
    	for (var i in a){
    		s += String.fromCharCode(a[i]);
    	}
    	return s;
    }
    
    var s1:String = stringToAscii("bob smith");
    var s2:String = stringToAscii("bob smith");
    
    trace(s1.length);
    trace(s1 == s2);
    Search first, asked questions later.

  9. #9
    Junior Member
    Join Date
    Jun 2009
    Posts
    15
    Inputting "bob smith" into Mod dog's function results in:

    98, 111, 98, 32, 115, 109, 105, 116, 104, 9, 10, 11, 12, 13...

    After the first 9 characters, it just counts up from there. While that gets us to 150 numbers, most names are under 10 or 15 characters so the first lot of numbers would be fairly unique but the remaining 140 or so would just be counting up to 150.

    Converting them to characters would result in a similar result, but using characters instead of numbers.

    In my original code, I have since learned from:

    http://www.republicofcode.com/tutorials/flash/as3loops/

    that:

    "An important thing to learn about loops is that defining a variable inside one makes that variable local, meaning that the variable and its value will disappear after each loop iteration. If a variable was defined inside a loop, you will get an error if you try to retrieve that variable from outside the loop because that variable does not exist outside it."

    This is my only problem. If I can solve the variable accessibility, I'm sorted.

  10. #10
    newb of many sorts Ralgoth's Avatar
    Join Date
    Apr 2002
    Posts
    466
    So not only are looking for a way to encode, but to decode as well? How is the data being used?
    Search first, asked questions later.

  11. #11
    Junior Member
    Join Date
    Jun 2009
    Posts
    15
    No, I don't need to decode.

    Without explaining all the complexities of the project... I want, from a persons name to create 150 circles on the screen in positions that are defined by their name. So, if Bob Smith types in his name, he gets 150 circles in positions that, if he types his name in again, will result in 150 circles in the exact same positions. However, if Jane Doe types in her name, she'll get 150 circles in different positions.
    Last edited by geniusprinting; 06-23-2009 at 08:46 AM. Reason: typo

  12. #12
    newb of many sorts Ralgoth's Avatar
    Join Date
    Apr 2002
    Posts
    466
    give this a try...

    Code:
    function stringToAscii(someString):String
    {
    	var arry:Array = someString.split("");
    	var s:String = "";
    	var num:uint = 0;
    	while(s.length < 150) {
    		for(var i:uint = 0; i<arry.length; i++){
    			s += String.fromCharCode(arry[i].charCodeAt(0)+num);
    			if(s.length == 150){
    				break;
    			}
    		}
    		num++;
    	}
    	return s;
    }
    
    var s1:String = stringToAscii("bob smith");
    var s2:String = stringToAscii("bob smith");
    
    trace(s1.length);
    trace(s1 == s2);
    trace(s1);
    it basically writes "bob smith" then shifts code and writes "bob smith" with a different char set, and repeats until you have a full string of 150

    so here's the trace output...

    150
    true
    bob smithcpc!tnjuidqd"uokvjere#vplwkfsf$wqmxlgtg%xrnym huh&ysoznivi'ztp{ojwj({uq|pkxk)|vr}qlyl*}ws~rmzm+~ xtsn{n,yu€to|o-€zvup}p.{w‚vq~q/‚|xƒwrr0ƒ}
    Last edited by Ralgoth; 06-23-2009 at 09:01 AM.
    Search first, asked questions later.

  13. #13
    Junior Member
    Join Date
    Jun 2009
    Posts
    15
    Me think I like that! Nice work.

    Problem is, how does it work. What's uint? Just googled it without much luck but given I had other questions about your code figured I'd ask you.

    I'm not familiar with your use of fromCharCode along with charCodeAt...

    Thanks

  14. #14
    newb of many sorts Ralgoth's Avatar
    Join Date
    Apr 2002
    Posts
    466
    uint is a whole number that is always positive. It processes quicker than using Number or int.

    charCodeAt is retrieving the unicode from each character in "bob smith"
    fromCharCode is converting the unicode back to a character
    by adding the uint, we shift the charcode over to get a new character each loop
    Search first, asked questions later.

  15. #15
    Junior Member
    Join Date
    Jun 2009
    Posts
    15

    Nice work

    Thanks to ralgoth and all for helping with this.

    I've implemented this into my code however just one one hurdle. The function is:

    function stringToAscii(someString):String

    The ":String" bit I guess makes the parameter, in this case someString a string.

    But what if I want other parameters in there?

    I need to add one numeric and one text parameter.

    Thanks again.

  16. #16
    newb of many sorts Ralgoth's Avatar
    Join Date
    Apr 2002
    Posts
    466
    The :String does indeed mean that the function will ALWAYS return a string. If you need it to return a number, it's probably best that you create another function. Or you can change it to return an Object, but you'll need to change the way you use the returned data...
    Code:
    function stringToAscii(someString:String, someNumber:Number):Object
    {
         var o:Object = new Object();
         o.myString = someString;
         o.myNumber = someNumber;
         return o;
    }
    
    var myObject:Object = stringToAscii("bob smith", 34);
    
    trace(myObject.myString);
    trace(myObject.myNumber);
    Search first, asked questions later.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center