in reply to build mysql data structure from text file

If you have control over the file format, i siuggest you change it:
<br
  1. Don't use whitespace as a delimiter. This is a broken concept. It will csues endless probelms when you copy-and-paste, change editors, etc. (If you REALLY like syntactically significant whitespace, you should be using Python.)
  2. Don't repeat each cate/subcat in the contained data. This is error-prone, redundant, and makes restructuring dificult.
Instead, why not XML?
#!/usr/bin/perl -w use XML::Simple; use Data::Dumper; $xml = ' <file> <cat1> <sub1> <sub-sub1/> </sub1> </cat1> <cat2/> <cat3> <sub1/> </cat3> </file> '; my $c = XMLin($xml); # substitute filename for $xml here print Dumper($c); __END__
Results:
$VAR1 = { 'cat1' => { 'sub1' => { 'sub-sub1' => {} } }, 'cat2' => {}, 'cat3' => { 'sub1' => {} } };
Bob Niederman, http://bob-n.com

Replies are listed 'Best First'.
Re: Re: build mysql data structure from text file
by fireartist (Chaplain) on May 13, 2003 at 22:07 UTC
    bobn,
    1. please see reply to rkg above.
    2. The actual category names don't follow the pattern I used as an example (cat1, sub1).
    I typed it like that so people could understand the relationships. I thought it'd be easier to read than a 'fruit' - 'apples', 'bananas' / 'snacks' - 'crisps', 'chocolate' type layout.
    Sorry for the confusion.
      Oh.

      Never mind.


      Bob Niederman, http://bob-n.com