|We don't bite newbies here... much|
kazak, we're going to need a whole lot more information than this in order to help you. Sometimes in the process of giving out that information, the answers become obvious, so don't be afraid to share.
My first question would be, how do I determine whether a data entry in a file is a key or a value?
Then I would think about how to write a regex that could detect a key, a value, and a key/value pair.
From your example data given, it's not clear why key5 and key 6 are empty, when you put value 5 and value 6 in your example. Shouldn't they go together? If not, how do you determine that a data value goes with a key?
Is the data somehow identifiable?
Is key5 and 6 empty because they appear alone? Is that what makes them empty?
Here's a thought:
Suppose you could detect key or value, would this pseudocodish outer loop help?
You'd then have to create the sub assign_key_value_from_current_line such that if only a key appears on the line, the value is set to undef and returned, and if the value appears then undef is stuffed into the first returned value for the key, and the system would use the first key. You may want an additional check in the main loop for the case where there's no key yet, but a bunch of values that would be tossed on the ground. In the case where both appear, the current key is reassigned and the new value likewise is recorded.
If you can't figure out difference between key and value, then it may be impossible, but from what you've given I don't know. Also start small, if you can't get all of the solution all at once, perhaps try a few simple steps, perhaps you can organize your data, or get it "part way" completed. In such a case, you might discover that it's good enough for what you're trying to do.
And as moritz notes, it'd be nice to see what you've tried already.