You are treating numbers as strings. Is it intentional that you want stringy-semantics on your comparisons? Regardless, the standard way to transform an array is via map. This isn't a job for tr, or for s///. If I understand your intent, you want to take an array of numbers, and constrain it such the all elements are 200 or less. Any element over 200 becomes 200.

my @array = ( 200, 201, 205, 194, 140, 250, 280 ); my @constrained = map { $_ > 200 ? 200 : $_ } @array; print "@array\n";

I understand you've been searching a long time on how to do this. Sometimes better than searching is asking yourself, "How would I do this on paper?" If I were doing it on paper, I would look at an element, and then make the decision, "Is the element greater than 200?" If it is, write down 200. If not, write down the original element. Great. Now we know how we would do it on paper; let's put it to code... and that produces what you see above.


Dave


In reply to Re: substituting values in an array by davido
in thread substituting values in an array by lafvcu1

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.