With your program Newfile is replaced by B set of values. How do I make it to take A set first and go to B set.
So what you have is two collections of numbers in the file you open as MYFILE. One set is labeled 'A' in column 3 and the other is labeled 'B' in column 3. The values in column 5 are an index, and the values in the last column are the ones you need to collect and add to NEWF.
What I said before is still mostly what you need to do. Here's a revised outline of the code you need to write:
Declare two arrays, one to hold the values from lines where column 3 is 'A' and another for lines where column 3 is 'B'. Something like:
my( @a_vals, @b_vals );
Read each line from MYFILE, use split to take it apart:
my( $type, $index, $value ) = ( split /\s+/ )[2, 4, -1];
Now you just put $value into the appropriate array by checking what $type is. An easy way to do that is with if/else, but I'm going to let you figure out the details. If you have Perl 5.10 or later, a nice way to handle this is with given/when.
Now change the while ( <NEWF> ) loop to insert the collected values where you want them. You get to figure out that part, as well.
You didn't show a sample of what the output should look like, so this is as far as I can go with what you've posted. If the output should have both 'A' and 'B' values at the end of a line, you do one thing. If you need two lines (one for each of the 'A' and 'B' values), you do something different. But we can't know unless you tell us. Which leads me to the following advice about how to get along well on PerlMonks:
Show your code. You did that. ++
Show sample data. You did that, but we had to ask more than once. And this last time, you posted way more data than necessary to show a typical sample. About five lines of each of the 'A' and 'B' types would have been enough.
Show the output you're getting and the output you want. You did that after we asked the first time, but when you came back with more questions and I asked for sample input and sample output, you posted input but no sample output.
I think you have enough additional information now to "fix" my code to do what you want. Give it a shot and come back with questions if you run into trouble.