#!/usr/bin/perl #use warnings; use strict; my @months = ( '','jan','feb','mar','apr','may','jun','jul','aug','sep +','oct','nov','dec' ); my @CUSTOMERS; # retain order of appearance in Crystal reports file. my %DATA; # hash, as suggested by fellow monks my $customer = ""; # holds current customer my ($month, $day, $value); while(<DATA>){ chomp; next unless $_; # skip empty lines # sorry, others did a better job with a simple split. if( m{^\s*(\d+)/(\d+)\s+([\d,\.]+)} ){ # matches a line with date and value ($month,$day,$value) = ($1, $2, $3); # 0+$month transforms string 02 into value 2 $DATA{$customer}{0+$month} = $value; }else{ $customer = $_; # next customer push @CUSTOMERS, $customer; # map{ $DATA{$customer}{$_}=0 } (1..12) # uncomment for zero's } } print join(";", @months). "\n"; for my $customer (@CUSTOMERS){ print $customer .";" . join( ";", map{ $DATA{$customer}{$_} } (1..12) ) . "\n"; } __DATA__ CUSTA 1/2015 100 10/2015 1,000 12/2015 1,000 CUSTB 02/2015 200 10/2015 1,000 12/2015 100

output:

;jan;feb;mar;apr;may;jun;jul;aug;sep;oct;nov;dec CUSTA;100;;;;;;;;;1,000;;1,000 CUSTB;;200;;;;;;;;1,000;;100

In reply to Re: Crystal Reports to CSV by FreeBeerReekingMonk
in thread Reformat RAW Excel Data by techjohnny

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, 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.