There is something strange with your code.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.