PDA

View Full Version : reading lines of text from... a textfile


Fledermaus
06-17-2008, 10:39 AM
Hi all,

I have a serious issue with reading a certain line from a textfile into a variable.

I'll try to explain: depending on what day of the week it is, line 1 or 2 ... should be loaded into a flash variable. Problem is that no weekday is mentioned in the txt file. Example of the textfile (excluding the '****'):

**************************
This is the first line of the textfile
And this the second
3th
And the fourth line
This would be the line for friday
This day I'm going out or so
Last line, best line
**************************

So, if the day is thursday, the string variable Todaystring should be loaded with the 4the line, so the value should be "And the fourth line".

Does anyone knows how this is possible? If it is possible...

Thanks for all your help!

regards,

gnomeontherun
06-17-2008, 05:22 PM
Die Fledermaus ist eine gute Oper!

Well the logical flow is to first take the text and split it into an array based on the new line character. If this doesn't work I would put a character like the pipe | between each line of text so it can be split based on that character.

dayArray:Array = loadedText.split("\n");

Then it will return dayArray with each line split. Then for thursday take dayArray[5]; and you've got it!

Fledermaus
06-19-2008, 02:36 PM
Mmm, ok, thanks for the quick answer !

But I think you are approaching the problem from a complete different perspective, and obviously a better one :cool:
I used loadvariables and such but it doesn't work.

According to your code, I presume I already loaded all the text into the variable loadedText, is that right?

dayArray:Array = loadedText.split("\n");

So I tried to load the text into that loadedText variable using:

loadedText = loadVariablesNum("mytext.txt", 0);

And then I created a textbox with the variable name loadedText.

But that doesn't seem to work out, I'll keep trying...

Fledermaus
06-19-2008, 02:45 PM
It's weird, the code you gave me returns a Syntax error:

dayArray:Array = loadedText.split("\n");

This is the code I'm currently trying out:

loadedText = new LoadVars();
loadedText.load("menukaart.txt");
dayArray:Array = loadedText.split("\n");

But neither loadedText nor dayArray works :-(

gnomeontherun
06-19-2008, 04:56 PM
Well the approach is still right, and the syntax must be the "\n" part. I mentioned in my first post I didn't know if that would work, but in most languages \n represents the Return character. So you should split your text file with another character like the Pipe |.

Lets get the text loaded correctly first and then worry about splitting it. Your text file needs something which will help us.

**************************
dailyText=This is the first line of the textfile|And this the second|3th|And the fourth line|This would be the line for friday|This day I'm going out or so|Last line, best line
**************************

So using a setup like that, you can more easily import, using the instructions below.

http://www.oman3d.com/tutorials/flash/loadvars_bc/

Fledermaus
06-23-2008, 04:30 PM
well, at last it worked, somehow:

in the textfile, I made the following lines:

&monday=mondayfile1 menusummer01.txt
&tuesday=tuesdayfile1 menusummer01.txt
&wednesday=wednesdayfile1 menusummer01.txt
&thursday=thursdayfile1 menusummer01.txt
&friday=fridayfile1 menusummer01.txt
&saturday=saturdayfile1 menusummer01.txt
&sunday=sundayfile1 menusummer01.txt

Then I load the variables using:
loadedText = loadVariablesNum(fileName, 0);

So when I assign a textfield to a variable (like monday) and I want to display it, it works. But now I want to assign the menu of today and the one of tomorrow to 2 variables, and that doesn't seem to work:

var dayMenu:String;
var dayMenuTomorrow:String;
switch (dayToday) {
case 0: dayMenu = sunday;
dayMenuTomorrow = monday;
break;
...and so on of course...

I think it has something to do with dayMenu being a string?! But I have no clue what it should be then? It's weird that I can't assign the value of the variables into strings?

Remark: it is in the same frame, so nothing to do with the timeline I think.

gnomeontherun
06-23-2008, 04:46 PM
Well now you have taken a new approach, which is that you are putting the & in front of your variables and making them each strings. It means that you are importing each line as a unique variable, instead of parsing the string like you were attempting at first. Sounds like a good alternative, nice work.

Fledermaus
06-23-2008, 05:22 PM
Yes indead, I followed your new approach thinking it would make things easier, like you suggested. But like you said:

...putting the & in front of your variables and making them each strings.

Then why can't I assign the variable monday to a new string, called dayMenu?

Just putting a textfield with var=monday works, but the conversion dayMenu=monday doesn't, keeps saying undefined??

I really don't get it.

gnomeontherun
06-23-2008, 05:43 PM
Couldn't you arrange it so that instead of passing the contents of the monday, tuesday, etc variables into another variable, to just pass them directly to the textbox? Example of how to put it directly into the Textbox using AS.

_root.textboxInstanceName.text = monday;

Fledermaus
06-23-2008, 06:01 PM
well, that doesn't seem to work:


switch (dayToday) {
case 0: _root.mealOfTheDay.text = sunday;
_root.mealOfTheDayTomorrow.text = monday;
break;
case 1: _root.mealOfTheDay.text = monday;
_root.mealOfTheDayTomorrow.text = tuesday;
break;
...


So I placed a dynamic textbox and assigned mealOfTheDay to it as instancename, but it keeps saying undefined. Seems like the variables are gone outside the whole actionscript thing?

gnomeontherun
06-23-2008, 06:11 PM
They are just not global, so inside of this function they are probably not getting referenced properly. Are you sure they are getting loaded properly? Run a trace on each of the variables after they are loaded to see if they are in Flash or not.

For a global variable you declare it like this

_global.variablename = value;

Or reference it like this instead

case1: _root.mealOfTheDay.text = _root.monday;

Fledermaus
06-24-2008, 08:00 AM
Well, for one or another reason, the variables read from the file don't seem to be accessible, unless you name a dynamic textfield to it. No "root-addressing, no nothing". Since you spent so much time to help me out, I'm gonna strip down the actionscript (should I say "script down" then?) into a new file to isolate the problem. And hopefully find a solution.

To do:

1. start a new fla
2. read variables from text
3. assign them to global variables

I will start that right now and post the whole actionscript right here.

:thumbsup: for all the help so far, we'll get there !

So, new file, new hope. This is the content of the textfile:


&monday=this is the monday string
&tuesday=this is the tuesday string
&wednesday=this is the wednesday string


And this is the actionscripting in the fla:


loadedText = loadVariablesNum("menukaart.txt", 0);
var menutoday = monday;
var menutomorrow = tuesday;


So I created 3 textfields, first with var monday, next var menutoday and the other menutomorrow. And the results are:

first line: this is the monday string
second line (menutoday): blanc, nothing in it, zero, empty, ...
third line (menutomorrow): undefined

Now I'm going to try the instance thing you suggested, which would perfectly do the trick.

Fledermaus
06-24-2008, 08:18 AM
ok, this is weird:

changed code to:


loadedText = loadVariablesNum("menukaart.txt", 0);
var menutoday = monday;
var menutomorrow = tuesday;
_root.instancetoday.text=monday;


And accordingly changed the second textfield to instancename menutoday without any text in the varfield. Now it shows in the textfield (brace yourselfs):


<p align="left"></p>


So... it seems to be an html-issue :confused:
I unchecked everything like render as html, draw box around it, ... but still it keeps talking about the align code. Might be a problem in the textfile then?

But that should be impossible since I'm using notepad to edit them...

(I tried to attatch the fla to this post but that doesn't seem to work)

Fledermaus
06-24-2008, 09:26 AM
UPDATE: I learned from mister Google that the loadvarsnum actually seems to be obsolete?!

I will try to find a different approach, like you, coding wizard, always suggest. To define the problem now is quite easy:

read vars from file and make them accessible as global vars throughout the fla.

I will keep you posted.

Fledermaus
06-24-2008, 01:44 PM
This is what the log says:

Variable _level0.mealOfTheDay = undefined
Variable _level0.mealOfTheDayTomorrow = undefined
Variable _level0.mondaystr = "mondayfile1 menusummer02.txt\r\n"
Variable _level0.tuesdaystr = "tuesdayfile1 menusummer02.txt\r\n"
Variable _level0.wednesdaystr = "wednesdayfile1 menusummer02.txt\r\n"
Variable _level0.thursdaystr = "thursdayfile1 menusummer02.txt\r\n"
Variable _level0.fridaystr = "fridayfile1 menusummer02.txt\r\n"
Variable _level0.saturdaystr = "saturdayfile1 menusummer02.txt\r\n"
Variable _level0.sundaystr = "sundayfile1 menusummer02.txt\r\n"


:confused:

Why is mealoftheday standing before the rest in the log?

gnomeontherun
06-24-2008, 04:20 PM
http://www.smartwebby.com/Flash/external_data.asp This will help with the loading question.

http://livedocs.adobe.com/flash/8/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00002334.html This gives you an example of how to handle a set of variables, to make sure they are loaded before trying to use them. It is possible that the variables are not yet available in the scope, so they don't register until after the code has been run the first time.