SOLVED! Thanks to everyone, especially ikegami, whose first solution in Re: Problems with matching? got the job done!! Original text follows....

I'm slurping in an entire file because I have data I want to substitute in more than one place. Thought it would be easier this way (one pass), but I'm either tripping on matching or making some silly mistake that I cannot see because I have been looking at the code for too long.

The goal is to replace network device names with random names -- basically, scrubbing the data before I send it to one of our vendors. But I am also trying to make sure the config file remains "well formed" so e.g. if a certain vendor going to be writing a parser for the entire file, it should work against the real thing.

Problem is as soon as I do the substitution, the next $old_name is the previous $new_full_name!! Probably missing something obvious here. :(

Same sample data:

unit 53 { description 1234-rwan-1; vci x.yy; } unit 54 { description 45ff-rwan-1; vci x.yz;
Here's the code:
# $1 $2 while( $file =~ m/^\s+description\s(\w\w\w\w)(-\w+-\w+);/gm ) { #say $1, $2 if $opt{debug}; # Way to reset $1, $2, etc? my $prefix = $1; my $suffix = $2; my $old_name = $prefix.$suffix; if( $old_name ~~ @old_names ) { # Should only be processing +each name once! say "ERROR: Already processed old_name $old_name."; reset; next; #<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< NEXT } push @old_names, $old_name; my $new_name = my_rand(); # Update: my_rand now takes care of +duplciates my $new_full_name = $new_name.$suffix; say "Replacing $old_name with $new_full_name..." if $opt{verbo +se}; # $file =~ s/$old_name/$new_full_name/g; reset; }
If I leave the substitution line above commented out I get (obviously the replacement is not actually happening):
Replacing 572l-rwan-1 with 4S1O-rwan-1... Replacing 25hu-rwan-1 with 9L8O-rwan-1... Replacing 51h2-rwan-1 with 2PDZ-rwan-1...
But if I uncomment the substitution line then I get the following. Note that the rlab-b3f2 also propagates, which is what makes me think it has something to do with matching (it's like $2 never changes).
Replacing 7301-rlab-b3f2 with 8SF6-rlab-b3f2... Replacing 8SF6-rlab-b3f2 with CAB7-rlab-b3f2... Replacing CAB7-rlab-b3f2 with 3X7L-rlab-b3f2...
Using a self-compiled Perl 5.10.1 on a Solaris 10 SPARC system. Anyone see anything obvious? Should I just do one pass to get the names I want to change and then read the file in again, line by line, making the substitution?

Update: The device names appear at least twice in the config file, hence why I was reading in the entire file. If it was a small file I may have just brute forced it, but there are around 8500 devices (so double that for the total number of substitutions) so I was hoping I could read it all into memory and pull it off that way.

Elda Taluta; Sarks Sark; Ark Arks


In reply to Problems with matching? by Argel

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.