in reply to Re: Concatenating arrays fetched from different text files
in thread Concatenating arrays fetched from different text files

This will work under the assumption that all input files have the same number of lines. However, if you have for example

Sample of text-1.txt files:

Line_1:Line_1_1 Line_2:Line_2_2 Line_3:Line_3_3 Line_4:Line_4_4
Sample of text-2.txt files:
Line_5:Line_5_5 Line_6:Line_6_6
Sample of text-3.txt files:
Line_7:Line_7_7 Line_8:Line_8_8 Line_9:Line_9_9 Line_10:Line_10_10
the array would have the content 'Line_9_9' and 'Line_10_10' in the second column (where one would expect content from the second file only). This is because push simply appends at the end of the array.

To avoid that it would be better to use fixed indexing instead of push:

So

my $i = 0; while (<>) { # read from @ARGV chomp; push @{ $value[$i++] }, /:([^:]+)/; # get timestamp $i = 0 if eof; }
could be replaced by this
my $i = 0; my $filecount = 0; while (<>) { # read from @ARGV chomp; $value[$i++]->[$filecount] = /:([^:]+)/; # get timestamp if (eof) { $i = 0; ++$filecount; } }

This would put the timestamps always in the right places, but you would have to be prepared to have 'holes' in the array, if the input files have different number of lines.

You can test if an array cell has a value assigned with code like the following:

if (!exists $value[$line]->[$file]) { # no value assigned, there is a hole in the array } else { # value assigned }
Hope this helps! Update: forgot a closing brace in the new code block. It is now fixed.

Replies are listed 'Best First'.
Re^3: Concatenating arrays fetched from different text files
by thanos1983 (Parson) on May 29, 2014 at 19:46 UTC

    To: hexcoder,

    More I read more I get impressed. Thank you for the tip it is always nice to learn possible problems that can be avoided.

    Thank you for your time and effort reading and replying to my question.

    Seeking for Perl wisdom...on the process...not there...yet!
      You're welcome. I got a lot of help here too.

        To: hexcoder,

        I keep saying that and maybe people got tired of it, but I feel the need to say that because it is not common unfortunately in these days. It is really nice that there are forums so well defined and nicely supported from people who do that without any profit. Beginners like me can learn so much here, and find so many new things. Thank you again for your time and effort.

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