in reply to Re^8: incrementing already existing file
in thread incrementing already existing file
Show us the code you tried. For that desired output, the modifications to the code I posted are not that complicated, especially with the suggestions that I already made. What I already said about collecting values from MYFILE will still work. Here are some additional hints to create the output you want:
-
For each line from NEWF, you need to check the index in column 5 and the A/B label in the last column. You get your hands on each of those with something like this:
my @fields = ( split /\s+/ ); # need to hold on to all field +s for later my( $index, $label ) = @fields[4, -1]; # this is an array slice
-
Now that you know the index and the label for a line, you use them to find the proper value from one of the arrays you created while reading from MYFILE (e.g., @a_vals or @b_vals if you followed my example). As I mentioned, before, you can use if/else to do that but a nicer way is with given/when (as long as your Perl is 5.10.0 or newer). Here's a suggestion but note that it will not work as written here. $x and $y are not declared. You need to replace them with the correct value from @a_vals or @b_vals (HINT: use $index from above).
use 5.010; given( $label ) { when ( 'A' ) { $fields[ -2 ] = $x; } when ( 'B' ) { $fields[ -2 ] = $y; } default { say "OOPS! bad label"; } }
-
Now you can print the result:
say join "\t", @fields; # separates fields with TAB
That's all there is to it. Give it another go and post back with your code if you need more help.
In Section
Seekers of Perl Wisdom