Some advice, as requested:
- Keep track of the current "chain of keys" as you progress through the lines.
- For each line, use the number of tabs to determine how many of the previously-saved keys you need to keep. Truncate your chain, use it to add a new entry to your hash at the right spot with the newly-read key, then add that key to your chain.
So your script would work like this:
- Read line 1. @keychain is empty. Zero tabs, so you truncate it to zero entries (a no-op, coincidentally). Progress through $hash along your @keychain; since it's empty, you're still at the root. Add an entry for 'one' to the hash, add 'one' to @keychain.
- Read line 2. @keychain contains 'one'. One tab, so you truncate it to one entry (another no-op). Progress through $hash along your @keychain; you'll end up at $hash->{'one'}. Add an entry for 'two' to the hash, add 'two' to @keychain.
- Read line 3. @keychain contains 'one' and 'two'. One tab, so you truncate it to one entry (NOT a no-op this time). Progress through $hash along your @keychain; you'll end up at $hash->{'one'} again. Add an entry for 'three' to the hash, add 'three' to @keychain.
- Read line 3. @keychain contains 'one' and 'three'. And so on...
You get the idea - this is how I'd approach this.