Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Parse config file into data strucuture and pass it between pages in CGI script

by sara2005 (Scribe)
on Sep 17, 2005 at 12:57 UTC ( [id://492857] : perlquestion . print w/replies, xml ) Need Help??

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

i have a data file in the following format:-

Page = first page [Start] varname = name1 varval = val1 etc.. etc.. [End] [Start] varname = name2 varval = val2 etc.. etc.. [End] etc.. etcc.. Page = second page [Start] varname = name11 varval = val11 etc.. etc.. [End] [Start] varname = name21 varval = val21 etc.. etc.. [End]

it goes on for several pages (may be, 20 at the max)

i decided to use array of hashses to hold the data. the logic is i read a line, if 'Page' is encountered, i store that in an array and get the page count, then, between [start] to [end], i read everything into a hash and push that into an array. note that i also add the 'Page' entry into the hash before pushing it into an array of hashes.

i was able to print the data for a given page after it was read successfully but my question is

(1) I want to create an input page with the data for a given page. is this a good approach?

(2) I need to be able to send the array of hashes from one page to another in CGI. (the logic is to have the complete data for various pages stored in an array of hashes with the pagenum and then decode it to build the input page for the selected one) Is it doable?

I would appreciate any suggestions.. (am i over-complicating things? one reason i went for this approach is bcoz i don't want to create a very complicated 3-tier or 4-tier structure!! but i am looking forword to you experts to tell me what is acceptable- obviously, i am new to Perl)

Thanks for the earlier tips using which i got a good start..

here is the code i wrote to decode the above data

open (FILE, "/somepath/temp.txt"); $inp_counter = 0; $var_counter = 0; while (<FILE>) { if ( /^Page.\s*=\s*(.*)/ ){ push @input_page_array, $1; $inp_counter = @input_page_array; } elsif ( /^Start*/../^End*/) { $line=$_; if ($line=~/^\s*(\w+)\s=\s(.*)/ ){ if ($1 eq "varname") { # if varname is found, and if it is not the first time, then push the hash into an array if ($var_counter != 0) { push @AoH, {%h}; } $var_counter++; %h = &initialize_struct( $1, $2, $inp_counter) } else { createpair( $1, $2); } } } } # push the final pair into the array push @AoH, {%h}; # get the elements in the array that correspond to the pagenum.. in th +is case, i am getting all elements for page 2 for $i ( 0 .. $#AoH ) { for $role (sort( keys %{ $AoH[$i] } )) { if ($role eq "pagenum" && $AoH[$i]{"pagenum"} == 2) { push @required, $i; } } } #print the elements for the selected page foreach $ii (@required) { for $role1 (sort( keys %{ $AoH[$ii] } )) { print "$role1=$AoH[$ii]{$role1}\n"; } print "\n\n"; } # subroutine to initialize the structure and add 'pagenum' and 'varnam +e' sub initialize_struct() { my ( $key, $value, $inp_cnt) = @_; my %hh; %hh = ( pagenum => "", varvame => "", varval => "", etc... , etc... ); $hh{$key} = $value; $hh{'pagenum'} = $inp_cnt; return %hh; }; # subroutine to populate rest of the data structure sub createpair{ my($key, $value) = @_; $h{$key} = $value; };

Considered by gmax: EDIT - change title to "parsing config file" (to tell apart from data structure), add code tags around data sample

Janitored by Arunbear: changed title and added code tags

Replies are listed 'Best First'.
Re: Parse config file into data strucuture and pass it between pages in CGI script
by wfsp (Abbot) on Sep 17, 2005 at 15:28 UTC
    You're nearly there!

    It would be worth considering putting your AoH into a hash using the pages as keys. You could then 'lookup' which page you want. You would still have a nice AoH to give HTML::Template.

Re: Parse config file into data strucuture and pass it between pages in CGI script
by CountZero (Bishop) on Sep 17, 2005 at 14:49 UTC
    I need to be able to send the array of hashes from one page to another in CGI.
    You would have to serialize your data in order to pass it along and that would get difficult to maintain and prone to errors. Such a thing just screams for session-management.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Parse config file into data strucuture and pass it between pages in CGI script
by Anonymous Monk on Sep 17, 2005 at 14:07 UTC
    what happens if open fails?

      well, this is not the complete code.. i meant to ask for some suggestions on the core functionality