Creating a variable from a text file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • skarr

    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"] = "Hearthston e",
    ["Color"] = "ffffffff",
    ["Tooltip"] = {
    [1] = "Hearthston e",
    [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\\Ico ns\\INV_Misc_Ru ne_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\\Ico ns\\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:1388 764288",
    ["Texture"] = "Interface\\Ico ns\\INV_Misc_Fo od_19",
    .....


    I tried this

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

    But it doesn't work. Any ideas ?

  • scotty

    #2
    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?

    Comment

    • Chung Leong

      #3
      Re: Creating a variable from a text file


      "skarr" <koen.willocx@t elenet.be> wrote in message
      news:1102933570 .420639.270590@ c13g2000cwb.goo glegroups.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"] = "Hearthston e",
      > ["Color"] = "ffffffff",
      > ["Tooltip"] = {
      > [1] = "Hearthston e",
      > [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\\Ico ns\\INV_Misc_Ru ne_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\\Ico ns\\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:1388 764288",
      > ["Texture"] = "Interface\\Ico ns\\INV_Misc_Fo od_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.


      Comment

      Working...