Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Solution Found! Thank you everyone who helped, here is my final part of the script that formats XLS:

... use strict; use warnings; use DBI; use POSIX; use File::Basename; use Net::SFTP::Foreign; use Text::CSV::Simple; use Spreadsheet::WriteExcel; ... # Generate XLS from array: my $strExcelFilename = "filename.xls"; my $workbook = Spreadsheet::WriteExcel->new($strExcelFilename); my $worksheet = $workbook->addworksheet("worksheetname"); my $intStartRow = 1; my $i = $intStartRow; my $intQueryArray = 0; my $o = $intQueryArray; my $intStartCol = 0; my $u = $intStartCol; $sth = execute_query("QUERY THAT RETURNS DATA FOR MULTIPLE COLUMNS IN +ONE COLUMN"); my %header = ( -bold => 1, -italic => 1, -text_wrap => 1, -border => 1, ); my $format = $workbook->add_format(%header); $worksheet->write('A1', "something", $format); $worksheet->write('B1', "something", $format); $worksheet->write('C1', "something", $format); $worksheet->write('D1', "something", $format); $worksheet->write('E1', "something", $format); $worksheet->write('F1', "something", $format); $worksheet->write('G1', "something", $format); # Format and split strings from query into their corresponding columns my @res; while ($res[$o] = $sth->fetchrow_array()) { my $set = $res[$o]; $set =~ s/[\(\)]//g; my @values = split(',', $set); foreach my $value (@values) { $worksheet->write($i, $u, $value); $u++ } $u = 0; $i++; $o++; }

Original Post

Hello all, I am pretty new to Perl but have coded in other languages and I have a problem. I have a data set coming back from a query that has all of the tables data in the first column. Each row in the first column has data like this:

(x,y,z,etc) (x,y,z,etc) (x,y,z,etc)

I need to split that data across to corresponding columns. Using "," as the delimiter between data, and removing the "()" wrapped around the results, producing something like this:

COL -> A B C D E F G ROWS-> 1 x y z . . . . 2 x y z . . . . 3 x y z . . . . 4 x y z . . . .

My code first puts the returnset from the query into an object $sth, sets the column headers, and formats them. Then I use:

... while (my @res = $sth->fetchrow_array()) { $worksheet->write($i, 0, $res[0]); $i++; } ...

This populates my excel file's first column with the data. I need help on how to place each (x,y,z,etc) into their own columns. There are also times when there is no data and a null or blank needs to be inserted. Can I do this when I am originally populating through my array while loop? Or should I populate like have already done then add in new code to split the data across the rows? I have done some searching online and found something similar to what I need:

... $/ = ''; my %list; while ( my $block = <DATA> ) { my ($tag, @list) = split /\n/, $block; s/^-- // for @list; $list{$tag} = \@list; } my $row = 0; foreach my $tag ( @{ $list{'main'} } ) { if ( exists $list{$tag} ) { $worksheet->write($row, 0, $tag); $worksheet->write_row($row++, 1, $list{$tag}); } } ...

But even after reading the perl regular expression documentation, I am still very confused as to how to read through this code and apply it to my own problem. Any help would be greatly appreciated ! -squarez


In reply to Using Perl and WriteExcel to split data from one column to many others by squarez

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (16)
    As of 2015-07-28 14:21 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (256 votes), past polls