I'm using a pair of arrays containing anonymous hashes to add URLs to some Item ID's for our stockroom's online catalog. To do this, I use the following code to add a key/value pair to those anonymous hashes:

$n=0; for my $el (@products) { if ( exists $products[$n]{"Vendor Item ID"} ) { $m = 0; for my $el (@config) { if ( exists $config[$m]{Vendor} ) { if ( $config[$m]{Vendor} =~ /$products[$n]{Name}/ ) { $url= $config[$m]{URL}; $url=~ s/\*/$products[$n]{"Vendor Item ID"}/g; $products[$n]{TaggedID} = $url; } } $m++; } } $n++; }

This works fine--as long as I don't use strict. The problem is that if I use strict, and declare the $url variable with 'my', running that substitution seems to clear out the variable, so that $products$n{TaggedID} gets set to nothing.

Why? And on a related note, is there a better way to do this? I'm still pretty new to perl.

For reference, one of the URLs looks like this: http://www1.qiagen.com/Search/Search.aspx?SearchTerm=*and the idea is to trade the * for whatever is in the 'Vendor Item ID' column, then store that in the appropriate hash.


In reply to 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.