You have an array @products which you loop over element by element, storing each element in the lexical variable $el, yet you never use this variable. Rather you use a variable $n to index into @products.
You do the same with the array @config with another lexical variable $el (which will make the other $el inaccessible) and $m.
Your loops will become much clearer if you drop the use of the index-variables and directly use the lexical variables.
These lexical variables are aliases for the elements of the arrays, so any change you make to them will really be made to the actual element of the array.
I have replaced your if tests by a next unless ... construct. If your arrays are quite regular, i.e. all elements contain an anonymous hash with respectively the 'Vendor Item ID' and 'Vendor' keys alway present, then you can even drop these two next unless ... constructs. Note: you are testing if these keys exist, not whether the value of the key exists or is defined. It could be empty or undef/ Maybe you should be testing for that?
Another comment concerns the test where you check to see whether the value of the product 'Name'-key matches the value of the 'Vendor' key in the config array. Are you sure you need a regex or would a simple test for equality (== or eq) suffice?
use strict; use warnings; for my $product (@products) { next unless exists $product->{'Vendor Item ID'}; for my $config_element (@config) { next unless exists $config_element->{'Vendor'}; if ($config_element->{'Vendor'} =~ $product->{'Name'}) { my $url = $config_element->{'URL'}; $url =~ s/\*/$product->{'Vendor Item ID'}/g; $product->{'TaggedID'} = $url; } } }
CountZero
Update: Put in proper dereferencing ('->').
A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
In reply to Re: Why does this code break if I properly set the scope?
by CountZero
in thread Why does this code break if I properly set the scope?
by stockroomguy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |