|
|
| Pathologically Eclectic Rubbish Lister | |
| PerlMonks |
The best way to replace several fragments of the file starting with the one pattern(marker) and ending with another pattern/marker.by likbez (Sexton) |
| on Nov 01, 2020 at 05:49 UTC ( [id://11123325]=perlquestion: print w/replies, xml ) | Need Help?? |
|
likbez has asked for the wisdom of the Perl Monks concerning the following question:
What esteemed monks think is the best, most elegant, most Perlish way to replace several fragments of the file starting with the one pattern(marker) and ending with another pattern/marker.
NOTES:
For example, in the example below, I want to replace fragment between markers AAAAAA and BBBBBB with the list ( 'f11','f12','f13') and the fragment between markers CCCCCC and DDDDDD with the list ("f21","f22"). aaa aaa AAAAAA ccc ddd BBBBBB 111 222 333 CCCCCC 444 555 666 DDDDDD 777 888 999Thanks in advance for any help. Again I am talking here not about the run of the mill solution (have three parallel arrays, for example, startmarker and stopmarker with markers and two dimensional (array of lists) replacement_list with the replacement fragments, find first starting marker in the file, loop till the end marker, inject the corresponding replacement fragment into output stream via the inner loop, and so on), but about the best, most elegant, way to accomplish this task in Perl that fully utilizes capabilities of the language. May be it is possible to adapt range operator to the task:
Which does allow to detect the start and end of the fragment and can be generalized to use multiple passes over the text to detect all fragments, but I am not sure that this is the optimal way. While this might serve as the base of the shortest solution to the problem, it is impossible to avoid multiple passes over the text.
I have only a very basic knowledge of this operator.
Back to
Seekers of Perl Wisdom
|
|
||||||||||||||||||||||||||||||