Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,360 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,254 people online right now. Registration is fast and FREE... Join Now!




Help, Concatination/array

 
Reply to this topicStart new topic

Help, Concatination/array

icedd
post 26 Sep, 2008 - 10:22 AM
Post #1


New D.I.C Head

Group Icon
Joined: 4 Mar, 2008
Posts: 42



Thanked 1 times

Dream Kudos: 50
My Contributions


Hey I have a stupid problem. This code wont work or me all it will output is (cat0,cat1,cat2,cat3,cat4,cat5) When I actually want it to output the valuse of those variables.

CODE
cat0=z;
cat1=a;
cat2=b;
cat3=c;
cat4=d;
cat5=f;

var my_array = new Array();
for(i=0; i<=5;i++){
    
    my_array[i] = ["cat"+i];
}
trace(my_array);
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 26 Sep, 2008 - 02:05 PM
Post #2


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,983



Thanked 78 times

Dream Kudos: 1175
My Contributions


If you want it to output the values and not the array try this:

CODE
var my_str:String = new String("");
for(i=0; i<=5;i++){
    my_str += " cat"+i;
}
trace(my_str);


Hope that helps.
User is offlineProfile CardPM

Go to the top of the page

Kerplope
post 27 Sep, 2008 - 07:56 AM
Post #3


New D.I.C Head

Group Icon
Joined: 22 Sep, 2008
Posts: 6



Dream Kudos: 100
My Contributions


Or, if for some reason you need the array, you could use another for loop to trace each of the values in the array.

CODE


for(var i=0; i<=5;i++){
    trace(my_array[i]);
}

User is offlineProfile CardPM

Go to the top of the page

DilutedImage
post 28 Sep, 2008 - 05:00 PM
Post #4


D.I.C Addict

Group Icon
Joined: 20 Nov, 2006
Posts: 642



Thanked 6 times

Dream Kudos: 25
My Contributions


You have two mistakes:

1. You've used array notation to access the 'cat' variables, without specifying the array. Because they were created in 'this' space, reference 'this'.

2. Without putting quotes around the letters, they're seen as undefined variables. Put the quotes around them and they'll be seen as String variables.

Here's your code, corrected:
CODE
cat0="z";
cat1="a";
cat2="b";
cat3="c";
cat4="d";
cat5="f";

var my_array = new Array();
for(i=0; i<=5;i++){
    my_array[i] = this["cat"+i];
}
trace(my_array);



QUOTE(BetaWar @ 26 Sep, 2008 - 03:05 PM) *

If you want it to output the values and not the array try this:

CODE
var my_str:String = new String("");
for(i=0; i<=5;i++){
    my_str += " cat"+i;
}
trace(my_str);


Hope that helps.


That doesn't help. This is the output of your code:
CODE
cat0 cat1 cat2 cat3 cat4 cat5


Please test your code before posting. biggrin.gif
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 28 Sep, 2008 - 05:41 PM
Post #5


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,983



Thanked 78 times

Dream Kudos: 1175
My Contributions


QUOTE
That doesn't help. This is the output of your code:
CODEcat0 cat1 cat2 cat3 cat4 cat5

Please test your code before posting.


I know exactly what it outputs.

QUOTE
When I actually want it to output the valuse of those variables

Right there he said he wants to output the value of the variables, not the arrays. That is what the code does, outputs the values of the variables not an array.

PS - If I don't test the code before posting I say so.
User is offlineProfile CardPM

Go to the top of the page

DilutedImage
post 29 Sep, 2008 - 11:34 AM
Post #6


D.I.C Addict

Group Icon
Joined: 20 Nov, 2006
Posts: 642



Thanked 6 times

Dream Kudos: 25
My Contributions


If you knew exactly what it outputs, then you've intentionally wasted everybody's time. I don't believe that's the case though. I believe you simply did not test your code (at least not well).

Your code creates a String object: var my_str:String = new String(""), and then concatenates the text " cat" to it: my_str += " cat", and then concatenates the string equivalent of the value assigned to i: +i. It never references the "values of those variables": this["cat"+i]. For this reason, the output of your code is the same with or without ever defining "those variables".

Run the code. Then change the "values of those variables" and run it again. Then delete "those variables" completely and run it again. It'll still produce the same text string. If you're unsure as to why, the answers can be found in the Help documentation.

My apologies if I've offended you. I appreciate all the help you provide in this forum, and encourage you to continue. But for the sake of those you help, please thoroughly test your solution before providing it.

This post has been edited by DilutedImage: 29 Sep, 2008 - 11:37 AM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 04:44AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month