473,386 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Creating a variable from a text file

Heya,

I have a text file which is basically an array declaration and I would
like to assign this to a array variable.

It reads like this (extract)

myProfile = {
["Blackhand"] = {
["Fingali"] = {
["Inventory"] = {
["Bag4"] = {
["Contents"] = {
[1] = {
["Quantity"] = 1,
["Name"] = "Hearthstone",
["Color"] = "ffffffff",
["Tooltip"] = {
[1] = "Hearthstone",
[2] = "Soulbound",
[3] = "Unique",
[4] = "Use: Returns you to The Crossroads. Speak to an
Innkeeper in a different place to change your home location.",
},
["Item"] = "6948:0:0:0",
["Texture"] = "Interface\\Icons\\INV_Misc_Rune_01",
},
[2] = {
["Quantity"] = 1,
["Name"] = "Skinning Knife",
["Color"] = "ffffffff",
["Tooltip"] = {
[1] = "Skinning Knife",
[2] = "One-Hand",
[3] = "2 - 5 Damage",
[4] = "(2.2 damage per second)",
},
["Item"] = "7005:0:0:0",
["Texture"] = "Interface\\Icons\\INV_Weapon_ShortBlade_01",
},
[3] = {
["Quantity"] = 7,
["Name"] = "Cactus Apple Surprise",
["Color"] = "ffffffff",
["Tooltip"] = {
[1] = "Cactus Apple Surprise",
[2] = "Use: Restores 61 health over 18 sec. Must remain seated
while eating. If you spend at least 10 seconds eating you will become
well fed and gain 2 Stamina and Spirit for 15 min.",
},
["Item"] = "11584:0:0:1388764288",
["Texture"] = "Interface\\Icons\\INV_Misc_Food_19",
.....
I tried this

$lua = urldecode($lua);
$thefile = implode("", file($lua));
$arr = ${$thefile};

But it doesn't work. Any ideas ?

Jul 17 '05 #1
2 1836
Your field definition is already there.

Why don't you just get rid of this: "myProfile = " , then do whatever
other adjustments you need to the text (if any)
and then assign the resulting string to your variable?

Jul 17 '05 #2

"skarr" <ko**********@telenet.be> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
Heya,

I have a text file which is basically an array declaration and I would
like to assign this to a array variable.

It reads like this (extract)

myProfile = {
["Blackhand"] = {
["Fingali"] = {
["Inventory"] = {
["Bag4"] = {
["Contents"] = {
[1] = {
["Quantity"] = 1,
["Name"] = "Hearthstone",
["Color"] = "ffffffff",
["Tooltip"] = {
[1] = "Hearthstone",
[2] = "Soulbound",
[3] = "Unique",
[4] = "Use: Returns you to The Crossroads. Speak to an
Innkeeper in a different place to change your home location.",
},
["Item"] = "6948:0:0:0",
["Texture"] = "Interface\\Icons\\INV_Misc_Rune_01",
},
[2] = {
["Quantity"] = 1,
["Name"] = "Skinning Knife",
["Color"] = "ffffffff",
["Tooltip"] = {
[1] = "Skinning Knife",
[2] = "One-Hand",
[3] = "2 - 5 Damage",
[4] = "(2.2 damage per second)",
},
["Item"] = "7005:0:0:0",
["Texture"] = "Interface\\Icons\\INV_Weapon_ShortBlade_01",
},
[3] = {
["Quantity"] = 7,
["Name"] = "Cactus Apple Surprise",
["Color"] = "ffffffff",
["Tooltip"] = {
[1] = "Cactus Apple Surprise",
[2] = "Use: Restores 61 health over 18 sec. Must remain seated
while eating. If you spend at least 10 seconds eating you will become
well fed and gain 2 Stamina and Spirit for 15 min.",
},
["Item"] = "11584:0:0:1388764288",
["Texture"] = "Interface\\Icons\\INV_Misc_Food_19",
....
I tried this

$lua = urldecode($lua);
$thefile = implode("", file($lua));
$arr = ${$thefile};

But it doesn't work. Any ideas ?


Try this:

$code = '$' . strtr($data, array(
'{' => 'array(',
'}' => ')',
'=' => '=>',
'[' => '',
']' => ''
));
eval($code);

Couldn't test this since the data you provided is truncated.
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Asha | last post by:
Greetings, I’ve query some data from the table, how can I push it into an excel file and allow user to save it? anyone done this sort of thing before?
13
by: Nagib Abi Fadel | last post by:
Is it possible to create a session variable for each user in Postresql ?? Thx
11
by: Michele and John | last post by:
I would like to write a C++ program that searches for the variable "state != 0" in a text file, and then go back 3 steps each time to read "count". The program should create a new file with "state ...
10
by: connyledin | last post by:
Im trying to create a version of the game Wumpus. Mine is called Belzebub. But im STUCK! And its due tuesday 2 maj. Im panicing! Can some one help me?? here is the file:...
7
by: rhitz1218 | last post by:
All: I'm trying to create a log file. I now I can be able to do that by doing the following: FILE *outfile; outfile = fopen("log.txt", "w"); fprintf("This is the text");
6
by: Chris Carlen | last post by:
Hi: I have an embedded system, platform: TI TMS320F2812 32-bit integer DSP. A module (.c file) contains external (to the funcs in that module) variables which are used to affect the operation...
10
by: fmsguy06 | last post by:
I have the numbers 1 through 50 in one line on a text file, in random order. I am trying to read that text file into an array, so as to copy the array and sort it. I can copy and sort arrays fine,...
3
Kelicula
by: Kelicula | last post by:
This is NOT a complete OO perl tutorial However I thought it could be beneficial to explain some of the basic concepts, and allow some users to simplify the software design process. I have...
1
by: BD | last post by:
This is probably fairly simple but I've looked at so much code an the last few days that my brain is fried. In the past I had an HTML page that contained a lot of code items and each item was...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.