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

Dear great ones, I have turned to the almighty Perl to make a spread sheet. It's made life incredibly easy for me up until now. I need to add data from files into columns. The problem is, I don't know how many columns there will be or how many files as the data I'm using for the files is variable. For example,

my $intStartRow = 0; my $altRow = $intStartRow; my $intStartCol = 2; my $altCol = $intStartCol; open (FRUITS, '/home/fruit_types.txt') or die ("Problem opening file: +$!"); # Fruit Header Insertion my $row = 1; while (<FRUIT>) { chomp; # Split on single tab my @Fld = split ('\t', $_); my $col = 3; foreach my $token (@Fld) { $worksheet->write($altRow, $altCol++, $token, $format); $col++; } $row++; }

So I have a file called fruit_types which contains the strings (apple, banana, orange, etc). The perl script will automatically put those strings into their own columns at the head of the spread sheet. The types of fruit will be variable so I will not know how many strings will be in the file, hence why it is added using the script above. I then have a directory with a separate file for each fruit i.e (apple.txt, banana.txt, orange.txt, etc). Inside each file contains spread sheet data (1,2,5, etc) to represent the amounts of each fruit I have. I now want to tie that data into the relevant column (apple, banana, orange, etc). The spread sheet would look like this...

apple banana orange 1 2 5 2 3 8 3 1 3
Can someone enlighten me how I would go about doing this? Thanks, Richard.

Replies are listed 'Best First'.
Re: Using variable files to add data into excel spreadsheet
by Laurent_R (Canon) on May 21, 2014 at 17:03 UTC
    If your data is not too large for that, you might consider reading all the files and populate a hash of hashes (or possibly a hash of arrays, or whatever data strructure is fit for your data). Once your are done with that, you know how many fruit types and other counters, and you can easily populate your spreadsheet. This might be slightly slower,but it is probably more practical.
Re: Using variable files to add data into excel spreadsheet
by RonW (Parson) on May 21, 2014 at 22:12 UTC

    Since you have a file for each fruit that is named for that fruit, you could loop on the fruit names, open the corresponding fruit file, then populate the column for that fruit.

      Got to love those Fruit loops™ !

              What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?
                    -Larry Wall, 1992

        Thanks for all the replies! Yes, can't beat fruity loops, ha, ha! Ron, I think that's what I'm trying to achieve. Essentially all I'm trying to do is repeat the while loop script for as many times as I have fruit_types. As you've probably guessed from my "fruity loops", primary school example, :) I just don't have the scripting knowledge of Perl to execute this. I've been modifying the script with a bash loop, with something along the lines of...

        occurrence=( $(wc -l fruit_types) ) for i in `seq 1 $occurrence` blah, blah, blah.

        I know there must be an easier and less primitive way of doing this like you've suggested but could someone throw an example my way. As said I need some help with the scripting.