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:
Here's the code:unit 53 { description 1234-rwan-1; vci x.yy; } unit 54 { description 45ff-rwan-1; vci x.yz;
If I leave the substitution line above commented out I get (obviously the replacement is not actually happening):# $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; }
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 572l-rwan-1 with 4S1O-rwan-1... Replacing 25hu-rwan-1 with 9L8O-rwan-1... Replacing 51h2-rwan-1 with 2PDZ-rwan-1...
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?Replacing 7301-rlab-b3f2 with 8SF6-rlab-b3f2... Replacing 8SF6-rlab-b3f2 with CAB7-rlab-b3f2... Replacing CAB7-rlab-b3f2 with 3X7L-rlab-b3f2...
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |