468,530 Members | 1,583 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,530 developers. It's quick & easy.

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 1708
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Asha | last post: by
13 posts views Thread by Nagib Abi Fadel | last post: by
11 posts views Thread by Michele and John | last post: by
7 posts views Thread by rhitz1218 | last post: by
6 posts views Thread by Chris Carlen | last post: by
Kelicula
3 posts views Thread by Kelicula | last post: by
1 post views Thread by BD | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.