Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I have the following problem: I have 400+ files with multiple lines in each file.Imagine something like the following:
File1 13 34 32 20 43 File2 22 15 27 33 48 65 34 File3 18 20 22 45 44 32 12 26

I want to be able to create a csv file, and put all data into columns, like an excel sheet.
Can you suggest how I should proceed? Am I thinking correct about an array of arrays? But how will I print the data afterwards? I would like to get something like:
File1	File2	File3
13	22	18
34	15	20
32	27	22
20	33	45
43	48	44
	65	32
	34	12
		26

Any tips?

Replies are listed 'Best First'.
Re: create array of arrays from multiple files
by GrandFather (Saint) on Jan 28, 2016 at 20:25 UTC

    Array of arrays is probably the best data structure. The trick in using it is to pull elements off the front of each nested array to get the output data as you need it:

    #use strict; use warnings; my $File1 = <<FILE; 13 34 32 20 43 FILE my $File2 = <<FILE; 22 15 27 33 48 65 34 FILE my $File3 = <<FILE; 18 20 22 45 44 32 12 26 FILE my @data; for my $file (\$File1, \$File2, \$File3) { open my $fIn, '<', $file or die "Can't open $file: $!\n"; push @data, []; while (<$fIn>) { chomp; push @{$data[-1]}, $_; } } while (grep {@$_} @data) { printf "%s\n", join "\t", map {$_ // ''} map {shift @$_} @data; }

    Prints:

    13 22 18 34 15 20 32 27 22 20 33 45 43 48 44 65 32 34 12 26
    Premature optimization is the root of all job security
      Thanks a lot Grandfather, the trick of printing was what I needed. As for you 2teez, I really don't understand your attitude. I explicitly said where I am stuck, so I needed help with that. You didn't want to help, fair enough, but please don't judge people you don't know.

        Where is the judgement? I don't see it. I simply refer you to a link that boldly stated among other things that ..Be precise about the correct behavior / desired output. Additional background about what you are trying to do and what you have tried also helps.

        If I don't want to help, I would not even put in a word to start with.
        I don't think "I want to .." like your OP says shows what you have tired and I don't think asking to shows some what of what you have tired a crime.
        We all came here either to help or to be helped, and then make the world a better place!

        Till you come out of the shadows, enjoy yourself friend!

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me
Re: create array of arrays from multiple files -- another oneliner using $.
by Discipulus (Canon) on Jan 28, 2016 at 21:59 UTC
    A simple oneliner can fill your @AoA (and is a perfect excercise before going to sleep!)

    The oneliner has a BEGIN block to copy @ARGV to the first row of the @AoA then use the current line number $. to choose wich column fill with the current $_ while the current starting column is governed by the $x which is increased every new file read (when eof is reached ARGV is also closed to reset $. ) The END block dump the @aoa in the desired format.

    I've changed your sample files (for my sanity) so that file1.txt only contains numbers starting with 1 and so on.

    perl -lnE "BEGIN{$aoa[0]=[@ARGV]}$aoa[$.][$x]=$_; if(eof){close ARGV;$ +x++}END{map{say join ',',@$_}@aoa}" file1.txt file2.txt file3.txt file1.txt,file2.txt,file3.txt 13,22,38 14,25,30 12,27,32 10,23,35 13,28,34 ,25,32 ,24,32 ,,36
    HtH

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: create array of arrays from multiple files
by hippo (Archbishop) on Jan 28, 2016 at 23:40 UTC
Re: create array of arrays from multiple files ( paste, csvpaste, pivot table)
by Anonymous Monk on Jan 29, 2016 at 01:32 UTC
A reply falls below the community's threshold of quality. You may see it by logging in.