Re: A good data structure to store INI files.



Marc Lucksch schrieb:
Maybe I'm an idiot here, but I can't figure this one out even if my life would depend on it:
Maybe I posted the question wrong, it is not about the INI parsers, I just didn't want to release another one...

Lets say I have this data structure, from an ini file, it cares for all the conditions that can happen (multiple sections, keys and values)

my $ini={
Ship=>[
{#First Ship
nickname=>[ #A key
['li_elite'] # value
],
fuse=>[ #multikey with multivalue
['intermed_damage_smallship01','0.000000','400'],
['intermed_damage_smallship02','0.000000','200'],

], #....
},{#Second Ship
nickname=>[
['li_elite2']
],fuse=>[
['intermed_damage_smallship01','0.000000','400'],
['intermed_damage_smallship02','0.000000','200'],

], #....
}, #A lot more 'Ships'
],Simple=>[
#A lot of Simples
],ship=>[
# 3 or 4 ships, sometime they are lowercase...
# That's a problem with this data model,
# Can't use Hash::Case::Preserve either,
# cause I would loose which ship's which.
],Pilot=>[
{
name=>[['pilot_corsair']],
}#Just one pilot entry
],
# lot more sections....
}

1. Problem:

I can't save the order the sections or keys came in, nor the names of them so I can't write an unmodified file out again. I would have to tie that structure.
I want to make is easily accessable:

$ini->{Pilot}->{name} instead of:
$ini->{Pilot}->[0]->{name}->[0]->[0].

for (@$ini) {
#in the order they came in, because a section can affect the
#following one. As seen in weapon_equip.ini, where [LODsomething.]
#before [Gun] affects the Gun LODs, I can't mix those up.
}
for (@{$ini->{Ships}}) {
#Return ships in order of appearance.
}

But I would need overload for that, and I could save it into an array for the order and a hash for quick lookup.

But I can't just return a hash with overload, because it wouldn't change my array on operations like

delete $ini->{newvalue}; (Change in array missing.
for (0 .. $#{$ini}) {
delete $ini->[$_] if something($_); #Change in Hash missing.
}
Which one do I trust now, hash or array?

or even

delete $ini->{Pilot};
$ini->{Pilot}=$otherpilot. #Order destroyed.

So I would have to return a tied hash, which gives me a lot of trouble with overload again. (See perldoc overload, last section or so)


.... No solution :( ...

Either I need a better way to save it, or I'm missing something here?

Marc "Maluku" Lucksch
.



Relevant Pages

  • Duck Typed Concepts for Ruby (was Re: A use case for an ordered hash)
    ... An Sequencable mixin can be defined that implements all sorts of operations such as append, concat, splice, sort, etc. ... extending an instance of Array with Sorted if the array is known to be sorted. ... Now returning to the thread at hand we can see that the difference between the associative array and hash hierarchies is that the hash hierarchy depends upon Hashable keys. ...
    (comp.lang.ruby)
  • Re: Suggestions for double-hashing scheme
    ... >> The items that are being moved are the items in the hash table itself, ... >> which are of fixed size (they are in an array, ... > typedef struct { ... One "uchar" aka 'unsigned char' is plenty to hold a probe ...
    (comp.programming)
  • [SUMMARY] Index and Query (#54)
    ... This was a fun quiz for me because I really don't know anything about indexing ... We see in initializethat the index is just a Hash. ... an Array of symbolic document names where the word can be found ). ...
    (comp.lang.ruby)
  • Re: Comment on how to uniquely track your objects in C# / hash table / get hash code
    ... Array, correct? ... This is largely irrelevant to the issue of performance, since hash ... where both insertions and lookups happen frequently, ... about fast lookups for balanced red/black binary trees. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: problem with hash & sort array
    ... Then I put the hash into an array with the sort ... The twist is I have to identify the duplicate ... a real data structure, and in the next place I make the exact opposite ...
    (comp.lang.perl.misc)

Loading