# program "mwlists2xls.pl" #-------------------------- # Extracts all lists from a maple worksheet (xyz.mw) # that *has been converted* to *plain text* before # with: [File] => [Export As] => xyz.txt (plain text) # # Usage: perl mwlists2xls.pl xyz.txt # # (this will create an excel file: xyz.xls) use strict; use warnings; use Spreadsheet::WriteExcel; my $fn = shift || 'noname.txt'; my $content; # - - - READ INPUT MAPLE FILE IN TXT FORMAT- - - - - # { open my $fh,'<', $fn or die "$!"; local $/; $content = <$fh> } # - - EXTRACT input lists of form 'list:=[[' - - - - # my $rgi = qr { ^ ( \w+ := \[ \[ \d [^\n]+ ) }xms; my @inplists = $content =~ /$rgi/g; # - - EXTRACT output lists of form: list := [0 - - - -# my $rgo = qr { ^ \s* ( \w+ \s+ := \s+ (?: \[[^\]]+ \] [\s,]+ )+ ) }xms; my @outlists = $content =~ /$rgo/g; printf "$fn: found %d input lists, and %d output lists\n", scalar@inplists, scalar@outlists; # - - - CREATE EXCEL workbook - - - - - - - - - - - - # my $xfn = ($fn=~/^[^\.]+/g)[0] . '.xls'; my $wb = Spreadsheet::WriteExcel->new($xfn); my $ws = $wb->add_worksheet( substr $xfn, 0, length($xfn)-4 ); my $fmt = $wb->add_format(); $fmt->set_bold(); $fmt->set_align('center'), $fmt->set_color('orange'); printf "writing into excel workbook %s\n", $xfn; # - - EXTRACT value/value pairs from individual lists - # my $rg = qr { \[ (\d[^,\s]+) ,\s* ([^\]]+) }xs; # - - UPDATE COLUMNS IN EXCEL FILE - - - - - - - - - - - # my ($col, %lists) = (0, ()); for my $list (@inplists, @outlists) { # extract list name from the first field of the list my $name = $1 if $list =~ /\s*(\w+)\s*:=/; next unless defined $name; my @data = $list =~ /$rg/g; ++$lists{$name} and $name = "${name}_$lists{$name}"; # make name unique printf "list $name has %d entries, ", scalar @data; my $row = 0; $ws->write($row, $col+0, $name.'_x', $fmt); # X HEADER $ws->write($row, $col+1, $name.'_y', $fmt); # Y HEADER ++$row; while( @data ) { $ws->write($row, 0+$col, shift @data); # X DATA $ws->write($row, 1+$col, shift @data); # Y DATA ++$row } print "table column $name created\n"; $col += 3 } # - - - - close EXCEL workbook - - - - # $wb->close(); printf "excel workbook %s closed!\n", $xfn;