OK, I've been thinking about the problem some more and have sort of an idea; what do you think about it:
1. define a list of all the LABELS I need and store them as variables $label1, $mylabel2 etc.;
2. with an if ... elsif loop, look for every variable $label1 and store it into a new variable; if it finds a match, this variable is defined, if not, it is undefined;
3. a number of if... statements: if variable defined, extract the data belonging to it, print the new lines, then the old data; if variable is undefined, print the new LIG instructions only.
Not sure if I have expressed this in an understandable way and if it can work, but I'll see whether I can cook up something. | [reply] |
I think your three-step plan is making things more complicated than they need to be. For one thing, the list of labels that you need (assuming that you really do need these) should be stored in a hash. Just figure out what piece of information about each label will make sense in your app as a hash key (i.e. the thing you'd want to use later for looking things up), and what information will be useful (if anything) as a hash value.
Use more than one hash if that makes things easier (but often, a single hash will do).
As for working out how to construct the output file, it would be better, if possible, for the changes to be made and output produced as you're reading the input. For example, if you somehow know that you need to insert a "(LIG O 51 ...)" line and you input a line that has "(LABEL O 52)", do you have enough information at this point to output the line(s) that should precede "(LABEL O 52)" ?
If you need to read the whole input before resolving that sort of problem, that's okay -- but again, it may be better to hold the input data in a suitable structure (AoH or HoA or somesuch) to reduce the risk of scrambling it beyond recognition.
You showed us an "easy" example in the OP, but you haven't given us a clear example of a "hard" case -- what it looks like on input, and what you'd like it to look like on output -- maybe I'm missing something, but this part isn't clear to me based on what you've said so far.
| [reply] |
Yes, I think you're tight about making things harder than necessary... I came up with this solution. It makes for ugly spaghetti code, I guess, but it seems to work(TM). First, I need to slurp in the entire file since I want to match across newlines:
my @lines = (<>);
my $text = join "", @lines;
Then, I test for each LABEL from LABEL O 0 to LABEL O 255. If the label exists, write it to another file, adding the new instructions. If it doesn't exist, just write the new instructions:
if ($text =~ m/( \(LABEL O 1\))(\n.+?)( \(STOP\)\n)/s) {
$labelo1 = $1;
$labelo1instructions = $2;
print "$labelo1\n NEWLIGINSTRUCTIONS HERE$labelo1instructions";
}
else {
print "(LABEL O 1)\n NEWLIGINSTRUCTIONS HERE\n";
}
I tried Tie::File, but couldn't get my head around it. And everything I tried with it was incredibly slow, so I went back to the brute-force approach...
Thanks for all your help!!
| [reply] [d/l] [select] |