Your problem is the regex you are using. Firstly, you need to negate the character class as [0-9] is going to match numbers, not the other was round. What you need is [^0-9]. Secondly, you should change the iterator from * (zero or more) to + (one or more). This is because zero or more digits will match at the beginning of the "null" element so you will get a replacement there resulting in "xraynull" and you will also change the "6" in the next array to "xray". Correcting the character class but leaving the iterator ([^0-9]*) will start to do the right thing with "null" but will make a mull of the first element of the next array resulting in "xray6".

Incorporating both changes with [^0-9]+ does the right thing. Refactoring your code to incorporate strictures and create the data structure in one go

use strict; use warnings; use Data::Dumper; my $raStruct = [ [ qw{null 44 4} ], [ qw{6 24 6} ], ]; print Data::Dumper->Dump([$raStruct], [q{raStruct}]); my $test = q{xray}; map { $_->[0] =~ s/[^0-9]+/$test/ } @$raStruct; print Data::Dumper->Dump([$raStruct], [q{raStruct}]);

gives the following output

$raStruct = [ [ 'null', '44', '4' ], [ '6', '24', '6' ] ]; $raStruct = [ [ 'xray', '44', '4' ], [ '6', '24', '6' ] ];

I hope this is of use.

Cheers,

JohnGG


In reply to Re: AoA Selective Element Substitution by johngg
in thread AoA Selective Element Substitution by awohld

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.