I’ve got a bunch of HTML form field data coming in as a hash, where each field name becomes the key & the field value is the hash value… your standard CGI module output from:
my $query = new CGI;
my %formdata = $query->Vars;
This time I’m working with a collection of multiple form fields that each have a numeric suffix (“name1” “name2” … “size1” “size2” etc). Is there a better way to use a counter to loop through the group of those in numeric order than this?
for (my $i = 1; $i < 10; $i++) {
print " Name $i: " . $formdata{"name$i"} . "\n";
print " Size $i: " . $formdata{"size$i"} . "\n";
}
…This isn’t bad but is there a simpler syntax? I.e. like this (but this doesn’t work – Can’t call method “name” without a package or object reference):
print " Name $i: $formdata{name$i}\n";
The obvious solution doesn’t work:
but can be fixed by replacing either of the sets of actual double-quotes
"..."with theqqoperator (qq{...}orqq(...)orqq/.../or whatever-you-like):See “Quote and Quote-like Operators” in the
perlopman-page.