Dear Monks,
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:
array-1[0] array-2[0]...array-n[0] array-1[1] array-2[1]...array-n[1] . . ... . . . ... . . . ... . array-1[n] array-2[n]...array-n[n]
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:
array-1 . ' ' . array-2 . ' ' . array-n
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).#!/usr/bin/perl use strict; use warnings; use Data::Dumper; $| = 1; my @timestamp; my @doc_read; my @result; my $write = 'write.txt'; my $line; my $text; my $arg; my $date = localtime(); sub read { foreach $arg (@_) { open (READ, "<" , $arg) or die ("Could not open: ".$arg." - $!\n"); while ( @doc_read = <READ> ) { chomp @doc_read; foreach $line (@doc_read) { @result = split (':', $line); push (@timestamp, $result[3]); } } close (READ) or die ("Could not close: ".$arg." - $!\n"); } return @timestamp; } sub write { open (WRITE , ">>" , $write ) or die ("Could not open: ".$write." - $!\n"); print WRITE "\n" . $date . "\n"; foreach $_ (@_) { print WRITE $_ . "\n"; } close (WRITE) or die ("Could not close: ".$write." - $!\n"); my $text = "Successfully writen on ".$write.".\n"; return ($text); } my @values = &read(@ARGV); print Dumper (\@values); my $final = &write(@values); print "\n" . $final . "\n";
Sample of text-1.txt files:
Line_1:Line_1_1:Line_1_2:Line_1_3:Line_1_4 Line_2:Line_2_1:Line_2_2:Line_2_3:Line_2_4 Line_3:Line_3_1:Line_3_2:Line_3_3:Line_3_4 Line_4:Line_4_1:Line_4_2:Line_4_3:Line_4_4
Sample of text-2.txt files:
Update, explanation of the 4th element.Line_5:Line_5_1:Line_5_2:Line_5_3:Line_5_4 Line_6:Line_6_1:Line_6_2:Line_6_3:Line_6_4 Line_7:Line_7_1:Line_7_2:Line_7_3:Line_7_4 Line_8:Line_8_1:Line_8_2:Line_8_3:Line_8_4
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:
$VAR1 = [ 'Line_1_3', 'Line_2_3', 'Line_3_3', 'Line_4_3' ];
Sample of output with two files as argument:
$VAR1 = [ 'Line_1_3', 'Line_2_3', 'Line_3_3', 'Line_4_3', 'Line_5_3', 'Line_6_3', 'Line_7_3', 'Line_8_3' ];
Thank you all for your time and effort reading and replying to my question.
Update: 2I 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.
#!/usr/bin/perl use strict; use warnings; use Data::Dumper qw(Dumper); use constant ARGUMENTS => scalar 2; $| = 1; my @timestamp_first; my @timestamp_second; my $write = 'output.txt'; sub first { open (FIRST, "<" , $ARGV[0]) or die ("Could not open: ".$ARGV[0]." - $!\n"); while ( my @first_read = <FIRST> ) { chomp @first_read; foreach $_ (@first_read) { my @result_first = split (':', $_); if (/^\s*$/) { # /^\s*$/ check for "blank" lines may contain s +paces or tabs next; } push (@timestamp_first, $result_first[3]); } } close (FIRST) or die ("Could not close: ".$ARGV[0]." - $!\n"); return @timestamp_first; } sub second { open (SECOND, "<" , $ARGV[1]) or die ("Could not open: ".$ARGV[1]." - $!\n"); while ( my @second_read = <SECOND> ) { chomp @second_read; foreach $_ (@second_read) { my @result_second = split (':', $_); if (/^\s*$/) { # /^\s*$/ check for "blank" lines may contain s +paces or tabs next; } push (@timestamp_second, $result_second[3]); } } close (SECOND) or die ("Could not close: ".$ARGV[1]." - $!\n"); return @timestamp_second; } sub write { open (WRITE , ">>" , $write ) or die ("Could not open: ".$write." - $!\n"); foreach $_ (@_) { print WRITE $_ . "\n"; } close (WRITE) or die ("Could not close: ".$write." - $!\n"); my $text = "Successfully writen on ".$write.".\n"; return ($text); } sub check { if (@ARGV < ARGUMENTS || @ARGV > ARGUMENTS) { die "Please enter ".ARGUMENTS." files to read!\n"; } } &check(@ARGV); my @first_values = &first($ARGV[0]); my @second_values = &second($ARGV[1]); my @final; my $num = @first_values; for($_ = 0; $_ < $num; $_++) { push (@final , $first_values[$_] . ' ' . $second_values[$_]); } print Dumper (\@final); my $date = time(); my $output = &write(@final); print "\nResult: ".$output."";
Output:
$VAR1 = [ 'Line_1_3 Line_5_3', 'Line_2_3 Line_6_3', 'Line_3_3 Line_7_3', 'Line_4_3 Line_8_3' ]; Result: Successfully writen on output.txt.
In reply to Concatenating arrays fetched from different text files by thanos1983
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |