Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Putting 2 variables together to make 1 variable...How? 3

Status
Not open for further replies.

TulsaJeff

Programmer
Jan 29, 2001
870
US
I am wanting to set up a calendar on my website and in doing so am using buttons to set the month variable as well as the year variable. I want these two to combine and make another variable which pulls that month/year info from a .txt file.

For example: I press the January button which sets calmonth to variable "jan"

I also press the 2002 button which sets calyear variable to "2002"

I have a text file in my root directory which contains info for variables "jan2001", "jan2002", "feb2001", "feb2002", etc. each with its own information.

What is the correct actionscript to accomplish this?

Ya' Gotta Love It!
sleepyangelsBW.jpg
 
Hi Jeff, it's as simple as this:

calmonth = "Jan";
calyear ="2002";
date = calmonth+calyear;
trace (date);

 
Thanks wangbar! I will give it a go. Ya' Gotta Love It!
sleepyangelsBW.jpg
 
ok...it is putting the variables together and opening an output box showing me that it is a valid variable... but it is still not pulling the data from the .txt file variable "jan2002".

It should be able to do that in test mode, right?

I have a frame action which looks like this:

loadVariables ("calendar.txt", _root.calendar_in);


If I put a simple textbox=jan2002; it will pull the data perfectly into my dynamic textfield named "textbox", however when I put the 2 variables together to construct the jan2002 it simply puts the concatenated variable into "textbox" and not the data from the text file.

I also tried textbox = Date; Ya' Gotta Love It!
sleepyangelsBW.jpg
 
Sorry Jeff, I misunderstood what you were trying to do. What I described is merely joining together the values of two variables not creating a new variable out of their constituent parts. I'll see if I can come up with something better...
 
Don't think you can dynamically change the variable of a given text box, which I believe you're sort of trying to do.
You should leave your text box variable allways the same.

Read your jan2002 text from your calendar.txt (separate different months with "&" if you only have one text file) and then assign your textfield variable to that value.

In other words, what you should read from the text file is:
&jan2002=Bla...Bla...Bla...
&feb2003=Bla...Bla...Bla...
&Etc...

Then in the movie simply set textbox = jan2002 or textbox=feb2003, according to the selection that was made!

Regards,
wink4.gif
ldnewbie
 
The problem is I have a button which allows the user to select the year as well as one to select the month.

1 button does not set the entire variable. the january button would set the "jan" and the "2002" button would set the 2002 part thus giving us jan2002 which I have in the text file as a variable.

I can do it another way using if statements which I have tried and it works but I was trying to set this up to be compatible for years to come without having to modify the code every year.

I really had it in my head that Flash would combine variable names to create a new variable and then be able to use that new variable dynamically. Maybe there is another round about way of accomplishing this. Let me know if anyone has any ideas. Ya' Gotta Love It!
sleepyangelsBW.jpg
 
Don't think that when using loadVariables, you can selectively load only one or a few variables, you're stuck with loading all of them. With that in mind, how about loading the text file in an date array and then working with an indexed date created from your two buttons? In anycase, some updating of the movie would be necessary at one point, if only to limit the number of years available at any given time. I imagine you're doing this for years to come (i.e from 2002 and on...), but you'll certainely have to limit the year button to some point, otherwise that text file is going to be huge!

Regards,
wink4.gif
ldnewbie
 
Ok! Asked one of my gurus on another forum, and he came up with this that seems to work wonderfully!

calMonth = "feb";
calYear = "2003";
selection = calMonth+calYear;
// To check selection:
trace (_root["selection"]);
loadVariables ("calendar.txt", "");
TextField1 = _root[selection];

Calendar.txt holds the following:
&jan2002=This is a test for january&feb2003=This is a second test for February 2003...&ETC...

Guess this deserves a second star... Too bad you won't be able to vote twice!
Regards,
wink4.gif
ldnewbie
 
old...you have outdone yourself once again.... I will give it a try. Thanks a million;-) Ya' Gotta Love It!
sleepyangelsBW.jpg
 
try eval(month + year);
it returns the variable "monthyear"
 
Old...did you build an example of this working? For some reason I am putting in exactly what you said and it is combining the variable and showing it in the trace but it is not pulling the variable from the .txt file.

rube... you will have to go into more detail. I understand eval and how it works but how do I then get it to pull my variable from the .txt file using your method?

thanks for all the help everyone!

tulsajeff@home.com Ya' Gotta Love It!
sleepyangelsBW.jpg
 
Yes Jeff, I did put something together! Will clean it up, and send it to you later... Or post a link to the .fla here on the forum.
No offense to Rube, but IMHO, I'd stay away from the eval() function! We did discuss this with Wangbar in a recent thread (which I can't find - with the marvelous search engine on this forum!), and I seem to recall that that was the consensus.

Catch you later,

Regards,
wink4.gif
ldnewbie
 
Here it is Jeff:

Of course I don't know what button setup you have or had in mind, and some of this code might not be necessary, in your case. But here are the elements, used the ones you need!

I've provided a default year (2002) in case you just select a month, before pressing display. I've also defaulted the month to January if you've only selected a year.
I've also included a "DATA NOT AVAILABLE!" message in case there isn't a variable (monthyear) in the calendar text file.

This is a 5 frame movie, to allow the loadVariables action to work properly.

You can download the zip package here:

Have fun & regards,
wink4.gif
ldnewbie
 
Old,

I think that is exactly what I need. For some reason when i click on the zip file it does nothing and does not ask me where I want to save it to. It is a possibility that it is my IE 6.0. It has been acting really screwy lately.

Is it possible to email just the flash file to me? I already have a .txt file with all the variables set up that I can tweak to use with your .fla file.

Thanks for all your help. Ya' Gotta Love It!
sleepyangelsBW.jpg
 
now i'm curious as to why eval() is dangerous! i haven't had any problems.

tulsa jeff: when you use loadVariables to bring in data from a textfile, say you have a variable called xy. if you want to find the value of xy, you can say eval(x + y). it's the same as saying, simply, xy. of course, as old says, there may be trouble with eval.
 
Jeffrey, e-mail sent!

As for you Rube, here's a copy of a post from the guru that got me on to the solution I proposed to Jeffrey.
See what he has to say about eval!
That said and posted, I guess you can still use it successfully, as people seem to continue to use the tellTarget action although it's deprecated in Flash 5.
In the end, it's all up to you!

"...Well, as far as I know, the only way to write a dynamic path reference (ie. using some variable to make the naming non-fixed) is by using an associative array.

The other alternative is totally evil and should be avoided, it looks like this:
eval("_root.pants"+ someNumber);

What eval does is truly evil... it spawns a new copy of the compiler engine for every time you call it - self modifying code. Not good. In JavaScript, you can actually do things like write dynamically executing code, but in Flash you cannot (otherwise the whole aScript compiler would have to be included with the plugin, which is bad and big). Anyway.
eval = BAD. (-:

Bear in mind that in the wonderful object oriented world of Flash goodness, that all objects are essentially arranged in an associative array - that is, an array that uses name pointers instead of numbers. So you can have

myArray[0] = "hello";
and
myArray.greeting1 = "hello";
i = 1;
trace(myArray["greeting"+ i]); // would return string "hello"

Both are acceptable ways of writing array values! Indeed, movieClips function exactly in this way. The dot operator is just a shorthand way of writing associative arrays! So

_root.myPath.toMy.movieClip
is the same as
_root["myPath"]["toMy"]["movieClip"]..."


Regards,
wink4.gif
ldnewbie
 
To clarify (or, more likely, confuse) the "eval" argument...

We were talking about it as a method for creating dynamic paths to control duplicated movieClips etc. In this case array notation is commonly held to be a better way to go (and certainly requires less typing).

However for banging a couple of strings together and sundry other functions "eval" is really powerful and not to be sniffed at. It's still not as powerful as the Javascript equivalent but useful nonetheless.

 
Thanks Everyone!

Old,

I received your email this morning but want have time to look at it until Thursday. I am knee-deep in a Facility layout for our investment company and it is requiring many long hours!

I will let you know later this week how it went;-) Ya' Gotta Love It!
sleepyangelsBW.jpg
 
Old,

I don't care what anyone says...you are the man! I took your .fla and was able to figure out exactly what makes it do what it does.

It is actually very simple as most things are and apparently once you concatenate the variables ... the new variable must be called inside of brackets for it to work.

Regardless, it works great.

I ended up making it automatically open to this month and this year and then the user can look back or forward if they so choose.

That was worth at least 5 stars in my opinion;-) Ya' Gotta Love It!
sleepyangelsBW.jpg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top