I have a file which contains following fields Application, Region and Server with their respective values like ABC, NA & Atom.prod. These fields are repeated to the file only the values are changed each time. I am trying to write a perl program that will read the entire txt fie and and search for the pattern Application, Region, Server and output these patterns and their values something like:

Application,ABC

Region,NA

Server,Atom.prod

The problem I am facing is that since these fields and values are in separate lines I am not able to put the output together in one line with ‘,’ comma separated. Can someone please help me on this? FYI – I am trying to use hash table for the output.

In order to get your code to work I also had to strip off the whitespace at the end of each line ( s/\s*$//; ). Was this not an issue for you when testing.

I opted for the compact / regexp approach, just to be less strict with regards to the input data format, in case the OP's real world data required considerable trimming / casing.

The OP actually contacted me with regards to this, and presented a more comprehensive input data example, which neither of our scripts covers. Certain key / value groups exist on the same line and some additional formatting was required. It also wasn't suitable to group values in a list ( there wasn't enough information provided to know how to handle this potential situation ) since "repeats" needed to be handled in a completely separate record, if that makes sense .

Uhhh, ignore my patronizing comment "your Perl has improved a lot in a short time! ". I got the wrong guy! I mixed you up with one of the beginners, who was making critical mistakes last week, and I was shocked at the improvement.

I understood that we only had the example input data to work from. I wanted to share with you the gist of what happened once the OP provided feedback via PM, particularly as you may not receive feedback yourself, giving closure to this potentially open ended thread ( inadvertently alleviating your forum pet hate ).

Its a common issue that the OP doesn't supply enough or the correct portion of data in order to cover every possible scenario, and if they do feedback, it can often take much discussion to eventually come up with the appropriate solution.

This OP has some difficult input data to deal with and was able to provide detailed requirements in later discussion.