Here's what I've done so far. Examples 1 and 2 do not work. Examples 3 and 4 work correctly.
Is there a simple way to accomplish this? or Should I use a Hash; and be done with it? or Simply not use strict? What's the downside in a relatively short program (350 lines)?use strict; use CGI; my $q = new CGI; # 1. The simple solution:... which will NOT work with 'strict'. Cannot + create variables on the fly ($$fieldname) @fields = $q->param; foreach $fieldname (@fields) { $$fieldname = $q->param($fieldname); # within this loop: also check for specific, or no data on each fi +eld } # 2. I applied 'strict' and I got the following errors. # Global symbol "varA" requires explicit package name... # Global symbol "varB" requires explicit package name... # etc for each variable.</i> # # So I tried this. The code ran without error, but nothing got assigne +d to the variables. my ($varC,$varB,$varA,$varD,.....); # an unsorted list of the expected + field names my @fields = $q->param; foreach my $fieldname (@fields) { no strict; $$fieldname = $q->param($fieldname); # within this loop: also check for specific, or no data on each fi +eld } # 3. A workable solution but, if the incomming data gets corrupted, it + could end up in the wrong variables. my @tempdata; my $i = 0; my @field = sort $q->param; # sorted list of the field names foreach my $fieldname (@fields) { $tempdata[$i] = $q->param($fieldname); # within this loop: also check for specific, or no data on each fi +eld $i++; } my ($varA,$varB,$varC,$varD,.....) = @tempdata; # $vars in sorted orde +r # 4. Using a hash which works as expected. my %data; my @field = $q->param; foreach my $fieldname (@fields) { $data{$fieldname} = $q->param($name); # within this loop: also check for specific, or no data on each fi +eld }
In reply to Creating variables while using 'strict' by nedals
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |