in reply to Concatenating arrays fetched from different text files

From the sketch of desired output stored in text file that you have shown, it looks like you want to transpose the rows and columns so that for all the files, index 0 is on the first line, index 1 is the second line (rather than column).

I tried the code below to get those results.

#!/usr/bin/perl use strict; use warnings; my @value; my $i = 0; while (<>) { # read from @ARGV chomp; push @{ $value[$i++] }, /:([^:]+)/; # get timestamp $i = 0 if eof; } my $write = 'write.txt'; open my $out , ">" , $write or die "Could not open: $write - $!\n"; print $out localtime . "\n"; print $out "@$_\n" for @value; close $out or die "Could not close: $write - $!\n"; print "Successfully written on $write.\n";
Hope this might be what you were looking for.

Replies are listed 'Best First'.
Re^2: Concatenating arrays fetched from different text files
by hexcoder (Curate) on May 29, 2014 at 10:56 UTC
    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.

      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.
Re^2: Concatenating arrays fetched from different text files
by thanos1983 (Parson) on May 29, 2014 at 09:16 UTC

    To: Cristoforo,

    Perfect, this is exactly what I need. You solved it in 20 lines and my code is 50-60 lines and does not even solve the problem. I was even thinking of making my code more complicated to solve the problem. Well the only thing that I need to figure out how to extract the 4th element of each array (e.g. array3). This is the location of the item that I need to extract.

    Thanks a lot for your time and effort, to assist me.

    Seeking for Perl wisdom...on the process...not there...yet!
      What do you mean by the 4th element of each array? From everything you said previously, you seemed to be willing to use all elements of the arrays. Or perhaps you mean that you need to use the 4th element of each line of the input files? Your requirement is not clear to me.

        To: Laurent_R,

        Apologies for the confusion, I did not explain from the beginning that I want to retrieve the 4th element of each array. I thought it would not make a difference since I was not even aware of perlre.

        After spending some time I manage to understand, that my problem could be solved by applying that simple solution. Unfortunately since they are new to me I am not feeling that comfortable with them. Never the less is always fascinating to learn new capabilities of programming languages.

        I will update my sample files to clear the miss understanding about the 4th element that I am trying to read.

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

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