setting variable name with variable at the end - perl -
$id = 1; foreach (@products) { $productid$id = param(product$id); ++$id; } i trying loop through products array , create new variable each item in products form uses checkboxes select products sale. tried use $productid$id , $productid"$id" returned syntax errors. there way achieve this?
i trying loop through products array , create new variable each item in products.
while possible create new variable each item, it's bad idea. have global, syntax ugly, , it's difficult pass variables around collection. instead, use hash or array. in case, since items indexed number , there's no gaps array makes sense.
my @product_params; foreach $product (@products) { push @product_params, param($product); } for each $product in @products, add param($product) @product_params. example, parameter $products[5] stored in $product_params[5].
since one-to-one mapping of @products parameters, easier , faster above map. mapping runs each element of list through function create new list. $_ contains each element of @products in turn.
my @product_params = map { param($_) } @products;
Comments
Post a Comment