in reply to Re^2: Using variable files to add data into excel spreadsheet
in thread Using variable files to add data into excel spreadsheet

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.

Replies are listed 'Best First'.
Re^4: Using variable files to add data into excel spreadsheet
by poj (Abbot) on May 22, 2014 at 11:28 UTC
    #!perl use strict; use Excel::Writer::XLSX; # Create a new Excel workbook and worksheet my $workbook = Excel::Writer::XLSX->new( 'fruits.xlsx' ); my $worksheet = $workbook->add_worksheet(); # get fruit types open FRUITS,'fruit_types.txt' or die "$!"; my @fruits = split '\s',<FRUITS>; # write column for each fruit for my $col (0..$#fruits){ my $fruit = $fruits[$col]; my $filename = $fruit.'.txt'; open IN, $filename or die "Could not open $filename : $!"; my @col = ($fruit); while (<IN>){ chomp; push @col, $_; } print "Writing column $col @col\n"; $worksheet->write_col(0,$col,\@col); } $workbook->close() or die "Error closing file: $!";
    poj

      Oh dear Lord, this is incredible!

      Thanks so much poj, for taking the time to write this. This is exactly what I'm looking for!

      With the risk of extracting the Michael...

      ...Any way of making the output to the spread sheet start at column 2 as the first columns in the spread sheet will already be accounted for? i.e

      column 0 column 1 column 2 column 3 column 4 shop area apple banana orange Aldi Glasgow 1 2 5 Tesco Edinburgh 5 6 3 Asda London 3 1 7
        # write column for each fruit for my $i (0..$#fruits){ my $col = $i+2; my $fruit = $fruits[$i]; .. etc
        poj