Octavian, I'd be surprised if it's not possible to generate variables of specific names on the fly in Perl, but I personally don't know how, and the syntax might be kind of weird. At the expense of a slightly more complicated data structure, I *can* give you some quick code that is hopefully simpler to follow anyway. (Besides, even once you've built your hashes with names obtained from the config file, how would the rest of your program know what those names were? It would have to assume particular ones, and that's not very versatile.) What *I* would build is a hash of hashes, where each first-level key is your "page" values ("assembly", "basic", etc.), and each of *their* values is another hash containing the rest of that section's definitions. So to access the assembly section's title would be $config{assembly}{title}, and so on. O'Reilly's "Programming Perl", chapter 9, has a good discussion of this kind of "complex data structure", and is where I learned about it. Here's some *UNTESTED* code to start you off:
# Begin code snippet # -------------------------- my %config; my $currSection = "none"; open( CONFIG, "yourfile" ) || die "Could not open yourfile\n"; while(<CONFIG>) { chomp; if(/^page=/ ) { # If new section, set $currSection to # everything after the match ($') $currSection = $' ; } elsif( /=/ ) { # Else, set a key/value in that section # using $` (pre-match text) and $'. $config{$currSection}{$`} = $'; } } close CONFIG; # -------------------------- # End code snippet Now, to access config variable "X" in each section, as I said above, use: $config{sectionname}{X} If you want to loop over all sections and print out all variables (basically regurgitate what you read), you could use (again untested): foreach $section (sort keys %config) { print "$section:\n"; foreach $key (sort keys $config{$section}) { print " $key = $config{$section}{$key}\n"; } } One caveat. Perl has something called by the somewhat scary name of "autovivification", which means something like "if the program uses a variable in a way that assumes its value is of a certain structure, and that value is actually undefined, then the appropriately structured value will automatically be constructed". I'm relying on this when I assign to $config{$currSection}{$`}, even though I haven't told it anywhere that $config{$currSection} will be a hash. As I say, I didn't test this code, so I could be completely off on this part of it. If you have problems, let us know and I can try to find time to actually test and debug it, or maybe some other nice person here could do so for us. Good luck! - John --- Sven <[EMAIL PROTECTED]> wrote: > Hi Teddy, > you may try the following: > > Write into your conf-file: > %assembly = ( > title => 'Assembly language page', > description => 'Download free manuals and > tutorials for assembly > language', > ..... > ); > > %basic = ( > title => 'Basic and Visual Basic > page', > description => 'Download Basic and Visual Basic > tutorials', > ..... > ); > > > And in your main-skript which should be called with > something like > "context=basic or assembly or ..." as a parameter: > use CGI; > $q = new CGI; > $context = $q->param(context); > > require "conf.txt"; > open(READ, "conf.txt") or die "Error opening file: > conf.txt, Errorcode: > $!\n"; > close(READ); > > if ($context eq "assembly") { %conf_data = %assembly > ; } > elsif ($context eq "basic") { %conf_data = %basic ; > } > > print $q->header; > print <<END_OF_HTML; > <HTML> > <HEAD> > <TITLE>$conf_data{title}</TITLE> > <META HTTP-EQUIV="Expires" CONTENT="0"> > </HEAD> > <BODY> > <H1>$conf_data{description}</H1> > </BODY> > </HTML> > END_OF_HTML > > > ----- Original Message ----- > From: "Octavian Rasnita" <[EMAIL PROTECTED]> > To: <[EMAIL PROTECTED]> > Sent: Saturday, May 25, 2002 4:06 PM > Subject: Getting content of a configuration file > > > > Hi all, > > > > I want to use a configuration file for a script > and maybe I look in > the > > wrong direction. Please tell me if I have a better > solution. > > > > I want to make a script that generates HTML pages. > I already have the > script > > but I need to manually enter the page title, the > keywords, the > description, > > a page header and a footer manually. > > > > I want to get these strings from a configuration > file. > > In this configuration file I want to have the > strings for all web > pages. > > > > In the configuration file I am thinking to have > something like: > > > > page=assembly > > title=Assembly language page > > keywords=assembly language code asm > > description=Download free manuals and tutorials > for assembly language > > header=This is the header text > > footer=This is the footer text > > > > page=basic > > title=Basic and Visual Basic page > > keywords=visual basic vb script ActiveX > > description=Download Basic and Visual Basic > tutorials > > header=This is the page header > > footer=This is the page footer > > > > .... > > > > Finally I would like to have something like: > > > > %assembly=(title=>'...', keywords=> '...', > description=> '...', > header=> > > '...', footer=>'...'); > > %basic=(title=>'...', keywords=> '...', > description=> '...', header=> > '...', > > footer=>'...'); > > .... > > > > However, I don't know if it is possible to > generate variables like > %assembly > > .... on the fly (because in that configuration > file I will add > variables for > > more pages. > > > > > > But maybe I am looking in a wrong direction and I > can have another > solution. > > > > I thought I could put in the configuration file > something like: > > > > assembly.title=Assembly language page > > assembly.keywords=.... > > assembly.description=... > > > > .... > > then to split the part before the = sign where is > the "." > > > > > > Please tell me if you have a better idea for such > a configuration file > I > > want. > > > > Thank you. > > > > Teddy, > > [EMAIL PROTECTED] > > > > > > > > -- > > To unsubscribe, e-mail: > [EMAIL PROTECTED] > > For additional commands, e-mail: > [EMAIL PROTECTED] > > > > > -- > To unsubscribe, e-mail: > [EMAIL PROTECTED] > For additional commands, e-mail: > [EMAIL PROTECTED] > ===== "Now it's over, I'm dead, and I haven't done anything that I want; or, I'm still alive, and there's nothing I want to do." - They Might Be Giants, http://www.tmbg.com __________________________________________________ Do You Yahoo!? Yahoo! - Official partner of 2002 FIFA World Cup http://fifaworldcup.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]