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]
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
In reply to Parse config file into data strucuture and pass it between pages in CGI script by sara2005
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |