Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a lua file I would like to transform into a perl hash. I load the file into a string and change the string where appropriate. Once I have my string formatted I want to assign it into hash(s). This is the resulting string shortened (as printed out by perl)
$VAR1 = ' "rpgoCPpref" => { "enabled" => "true", "honorold" => "true", "debug" => "false", "questsfull" => "false", "tooltipshtml" => "true", "lite" => "true", "scan" => { "inventory" => "true", "talents" => "true", "honor" => "true", "reputation" => "true", "spells" => "true", "pet" => "true", "equipment" => "true", "professions" => "true", "mail" => "true", "skills" => "true", "quests" => "true", "bank" => "true", }, "ver" => "10500", "reagenthtml" => "true", "talentsfull" => "true", } "myProfile" => { .... blah blah ..... }'
This is the string from the file before modification:
rpgoCPpref = { ["enabled"] = true, ["honorold"] = true, ["debug"] = false, ["questsfull"] = false, ["tooltipshtml"] = true, ["lite"] = true, ["scan"] = { ["inventory"] = true, ["talents"] = true, ["honor"] = true, ["reputation"] = true, ["spells"] = true, ["pet"] = true, ["equipment"] = true, ["professions"] = true, ["mail"] = true, ["skills"] = true, ["quests"] = true, ["bank"] = true, }, ["ver"] = 10500, ["reagenthtml"] = true, ["talentsfull"] = true, } myProfile = { .... blah, blah ..... }
so now that I have this string How do I access it as a hash?

Replies are listed 'Best First'.
Re: Help with making a string into a hash
by liverpole (Monsignor) on Oct 24, 2006 at 13:39 UTC
    I have no idea what a "lua" file is, but ...

    If you use the wonderful Data::Dumper module, it's very simple to save and restore your data structures from a file. For example:

    use strict; use warnings; use Data::Dumper; our $phash = { "enabled" => "true", "honorold" => "true", "debug" => "false", "questsfull" => "false", "tooltipshtml" => "true", "lite" => "true", "scan" => { "inventory" => "true", "talents" => "true", "honor" => "true", "reputation" => "true", "spells" => "true", "pet" => "true", "equipment" => "true", "professions" => "true", "mail" => "true", "skills" => "true", "quests" => "true", "bank" => "true", }, "ver" => "10500", "reagenthtml" => "true", "talentsfull" => "true", }; # Save to a file my $fh; my $fname = "data.txt"; open($fh, ">", $fname) or die "Cannot write to '$fname' ($!)\n"; my $d = Data::Dumper->new([$phash], ['phash']); print $fh $d->Dump(); close $fh; + # Time passes ... # Make the hash null, to verify that it's read from the file correctly $phash = 0; printf "Results of hash = %s\n", Dumper($phash); + # Read back the file do $fname; printf "Results of hash = %s\n", Dumper($phash);

    Note the usage of our $phash, so that the $phash = ... in the output file will be in the same scope as the version of $phash which is in the script.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      I have no idea what a "lua" file is, but ...

      Lua is a programming language. I suppose Anonymous Monk means some Lua source file.

        Yes.. Lua is a programming language and the variables are stored as tables simular to a perl hash. Thus converting between the Lua table to a perl Hash sould be reather simple with some reformatting.
      Well its a start( work in progress ) but thanks for the help guys hopefully, I can get this more generic as I go.
      #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $input; my %luahash = (); my @temp; unless (open (FILE,"CharacterProfiler.lua")) { print "Unable to open file."; exit; } my@ainput = <FILE>; close FILE; $input = join("", @ainput); $input =~ s/\[\"(\w+)\"\]/\"$1\"/g; $input =~ s/=/=\>/g; $input =~ s/(\[)(\d|\d\d)(\])/"$2"/g; $input =~ s/(\d+|\d+\.\d+),/"$1",/g; $input =~ s/(\w+) =\> \{/"$1" => \(/g; $input =~ s/(true|false)/"$1"/ig; $input =~ s/\}\,\n$/\}/; $input =~ s/\n\}\n/\n\)\n/g; @temp = split(/\n\)\n/,$input); my $inx = 0; foreach (@temp) { $temp[$inx++] .= "\n)"; } $temp[0] =~ s/\"\w+\"\s=\>\s\(/\(/; %luahash = eval($temp[0]); print Dumper(%luahash);
Re: Help with making a string into a hash
by Fletch (Bishop) on Oct 24, 2006 at 14:27 UTC

    Not exactly a direct solution, but I wrote this to turn Lua (from the same source program as yours, coincidentally :) into YAML.

    You can open a pipe from that and then have YAML do the work for you. Zug zug.

      Thanks again guys.. here is a working program.. now I can finally access the data easily.
      #!/usr/bin/perl -w use strict; use warnings; use Data::Dumper; my $input; #my $1i; my %luahash = (); my @temp; unless (open (FILE,"CharacterProfiler.lua")) { print "Unable to open file."; exit; } my @ainput = <FILE>; close FILE; $input = join("", @ainput); $input =~ s/\[\"(\w+.+)\"\]/\"$1\"/g; $input =~ s/=/=\>/g; $input =~ s/(\[)(\d|\d\d)(\])/"$2"/g; $input =~ s/(\d+|\d+\.\d+),/"$1",/g; $input =~ s/(\w+) =\>/"$1" =\>/g; $input =~ s/=\> (true|false)/=\> "$1"/ig; $input =~ s/\}\,\n$/\}/; $input =~ s/\n\}\n\"/\n\},\n\"/g; my $temp = '(' . $input . ')'; %luahash = eval($temp); exit;
Re: Help with making a string into a hash
by blazar (Canon) on Oct 24, 2006 at 14:00 UTC
    This is the resulting string shortened (as printed out by perl)

    I think you rather mean "as (would be) printed out by Perl's Data::Dumper", In which case it is nearly trivial to make that into a perl structure by simply evaluating the string, although you probably want to get rid of $VAR1 there. Since you explicitly write hash rather than em>hashref, you may want change the outermost curlies into parens as well.

    PS: I always say "beware of (string) evals, but this is a situation in which it would definitely be useful. The usual caveat does apply, though.