Dear Monks,

This is my second question for today, so my apologies for that but no matter what I tried I was not able to find the solution that I was looking for.

I have created a script that takes *.text file(s) as argument input(s) and process them. So far the script works fine but I can not get the desired output.

I want to have the content of the files printed in concatenation based on line by line of each file.

Update on the output.

Sample of desired output printout:

1 Line_1_3 Line_3_3 Line_4_3 2 Line_2_3 (empty) Line_5_3 3 (empty) (empty) Line_6_3 4 (empty) (empty) Line_7_3 . . . . . . . . . . . . n Line_n_3 Line_n_3 Line_n_3[n]

Text file sample of sample.txt:

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

Text file sample of sample_2.txt:

Line_3:Line_3_1:Line_3_2:Line_3_3:Line_3_4

Text file sample of sample_3.txt:

Line_4:Line_4_1:Line_4_2:Line_4_3:Line_4_4 Line_5:Line_5_1:Line_5_2:Line_6_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

Each file contains data, I import the data on arrays then I short them on array of arrays. I thought that by inserting them into an array by separate arrays, will be much easier for shorting after. But due to lack of experience with array of arrays I manage to find my self in a deadened.

Final update with Solution.

First of all I want to say thank you to Cristoforo for his assistance and he helped to find the solution to my problem.

Working code of array of arrays:

#!/usr/bin/perl use strict; use warnings; use Text::Table; use Data::Dumper; use List::Util qw{max}; use Fcntl qw(:flock); # import LOCK_* and SEEK_END constants $| = 1; my (@result , @values , @array); sub array_sub { foreach my $arg (@_) { open (READ, "<" , $arg) or die ("Could not open: ".$arg." - $!\n"); flock(READ, LOCK_EX) or die "Could not lock '".$arg."' - $!\n"; if (-z "".$arg."") { print "File '".$arg."' is empty!\n"; # -z File has zero size (is empty). } my @doc_read = <READ>; chomp @doc_read; foreach $_ (@doc_read) { @result = split (':', $_); if (/^\s*$/) { # /^\s*$/ check for "blank" lines may contain s +paces or tabs next; } push @values, $result[3]; # get timestamp } # push an array to another to create 2-dimentional array push (@array, [@values]); @values = (); close (READ) or die ("Could not close: ".$arg." - $!\n"); } my $max_idx = max map $#$_, @array; =table structure If no columns are specified, the number of columns is taken from t +he first line of data added to the table. The effect is as if you had speci +fied Text::Table->new( ( '') x $n), where $n is the number of columns. =cut my $tb = Text::Table->new; =loop values $i = maximum number or array values $_ = maximym number of characters =cut foreach my $i (0 .. $max_idx) { $tb->add( map $array[$_][$i], 0 .. $#array); } return $tb; } my $a_table = array_sub(@ARGV); print $a_table;

Sample of output:

$VAR1 = [ [ 'Line_1_3', 'Line_2_3' ], [ 'Line_3_3' ], [ 'Line_4_3', 'Line_5_3', 'Line_6_3', 'Line_7_3' ] ]; Line_1_3 Line_3_3 Line_4_3 Line_2_3 Line_5_3 Line_6_3 Line_7_3

Working code of array of hashes:

#!/usr/bin/perl use strict; use warnings; use Text::Table; use Data::Dumper; use List::Util qw{max}; use Fcntl qw(:flock); # import LOCK_* and SEEK_END constants $| = 1; my $value = (); #create anonymous string_ref my %hash = (); sub hash_sub { foreach my $arg (@_) { open (READ, "<" , $arg) or die ("Could not open: ".$arg." - $!\n"); flock(READ, LOCK_EX) or die "Could not lock '".$arg."' - $!\n"; if (-z "".$arg."") { print "File '".$arg."' is empty!\n"; # -z File has zero size (is empty). } my @doc_read = <READ>; chomp @doc_read; foreach $_ (@doc_read) { my @result = split (':', $_); if (/^\s*$/) { # /^\s*$/ check for "blank" lines may contain s +paces or tabs next; } push (@$value , $result[3]); } $hash{$arg} = $value; $value = (); # emptying the string_ref for the next ARGV close (READ) or die ("Could not close: ".$arg." - $!\n"); } my $max_idx = max map $#$_, values %hash; my $tb = Text::Table->new; for my $i (0 .. $max_idx) { $tb->add( map $hash{$_}[$i], sort keys %hash); } return $tb; } my $h_table = hash_sub(@ARGV); print $h_table;

Sample of output:

$VAR1 = { 'sample.txt' => [ 'Line_1_3', 'Line_2_3' ], 'sample_2.txt' => [ 'Line_3_3' ], 'sample_3.txt' => [ 'Line_4_3', 'Line_5_3', 'Line_6_3', 'Line_7_3' ] }; Line_1_3 Line_3_3 Line_4_3 Line_2_3 Line_5_3 Line_6_3 Line_7_3

Maybe is extremely easy but I can not find a way to do it. I was reading the ARRAYS OF ARRAYS and the ARRAYS OF HASHES but no matter how many different approaches I tried I did not manage to get the desired output.

Any help is much appreciated.

Thank you all for your time and effort answering my question.

Seeking for Perl wisdom...on the process...not there...yet!

In reply to How to concatenate a generic array of arrays and an array of hashes by thanos1983

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.