in reply to Help with making a string into a hash

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$..$/

Replies are listed 'Best First'.
Re^2: Help with making a string into a hash
by blazar (Canon) on Oct 24, 2006 at 14:04 UTC
    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.
Re^2: Help with making a string into a hash
by Anonymous Monk on Oct 24, 2006 at 17:07 UTC
    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);