|P is for Practical|
Concatenating arrays fetched from different text filesby thanos1983 (Vicar)
|on May 28, 2014 at 23:21 UTC||Need Help??|
thanos1983 has asked for the
wisdom of the Perl Monks concerning the following question:
I am not sure if the title can explain or at least describe partially what I am trying to implement, so I will wright a short explanation.
I have created a script that reads files as argument and push the result in an array. Sample of code will be provided a bit further on.
My target is to provide several file names as argument (e.g. > 2), where I will be able to store them on separate arrays in order to concatenate them after.
Sketch of desired output stored in text file:
With the current configuration of my code, I am able to read the file given as argument, parse it into an array, choose the element of the file that I am interested and then write the output of the file.
For one file as input works just fine. The script is able to read several inputs and execute the process for different text files accordingly.
The problem is that I am trying to concatenate two arrays in one simultaneously. What so I mean, each file will have n rows which I need to process and create different arrays so I could concatenate them after on the desired result.
I was thinking if I had different arrays I could apply:
Or something close to that solution, I have not implemented that before so I need to figure it out how to do it for each array element.
Well I was thinking about creating several subroutines with different array names and then at the end concatenating the arrays. But, this is not a "good" coding solution, not to mention that my solution will not be generic, it will always require modifications in case of additional text.txt file additions.
In conclusion, I want to believe that it can be done in a single subroutine by somehow modifying the name of the array and as a second step concatenating the arrays together. Or maybe I am thinking crazy here.
Sample of running code:
Update on the sample text files. (Adding extra length of the array).
Sample of text-1.txt files:
Sample of text-2.txt files:
Update, explanation of the 4th element.
Apologies for the confusion that I caused. I was not ware that by applying perlre can solve my problem. My initial plant was to extract the 4th element of each line on each file.
The way that I could imagine of solving it, was to read each line separately, use the split function to remove the : (colon) and store them into an array. In order to extract the 4th element of the array (information) that I want to use.
This is how I thought that could be a possible solution.
Sample of output with one file as argument:
Sample of output with two files as argument:
Thank you all for your time and effort reading and replying to my question.Update: 2
I have found a solution to my problem, but I know that it is not generic and not well coded. But for the moment it meets my expectations, any improvement would be much appreciated.
The code provided underneath must to be loaded with the test *.txt files mentioned above.
Seeking for Perl wisdom...on the process...not there...yet!