in reply to Using variable files to add data into excel spreadsheet

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.

  • Comment on Re: Using variable files to add data into excel spreadsheet

Replies are listed 'Best First'.
Re^2: Using variable files to add data into excel spreadsheet
by NetWallah (Canon) on May 22, 2014 at 05:01 UTC
    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.

        #!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

        Ha, ha, yes quite, indeed. Forgive me mien Fuhrer, I am but a mere foot soldier in the ranks of the grammar Nazi's, I still slip from time to time! ;)

        On a serious note, thanks for your help.