Connecting Tech Pros Worldwide Help | Site Map

Creating a variable from a text file

  #1  
Old July 17th, 2005, 10:59 AM
skarr
Guest
 
Posts: n/a
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 ?

  #2  
Old July 17th, 2005, 10:59 AM
scotty
Guest
 
Posts: n/a

re: Creating a variable from a text file


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?

  #3  
Old July 17th, 2005, 11:00 AM
Chung Leong
Guest
 
Posts: n/a

re: Creating a variable from a text file



"skarr" <koen.willocx@telenet.be> wrote in message
news:1102933570.420639.270590@c13g2000cwb.googlegr oups.com...[color=blue]
> 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 ?
>[/color]

Try this:

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

Couldn't test this since the data you provided is truncated.


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delimited Text File Export grumpydadtl answers 4 August 28th, 2008 06:44 PM
String Search in a LARGE text file jcrouse answers 6 November 20th, 2005 08:08 PM
How should I construct my objects when reading ctor args from a text file? Eric Lilja answers 0 July 23rd, 2005 02:06 AM
Writing to a text file Neil Trigger answers 10 July 19th, 2005 05:51 AM