Ok, taking a stab at what you seem to want as the output then the following code seems to do it.

my (@dates, %msc_sequences, %date_totals); <INFILE>; # skip first line while( <INFILE> ) { chomp; # use variable names as clearer than hash futher down my ($msc_name, $chunum, $date, $count_per_date) = split /\s*,\s*/; push @dates, $date; $date_totals{$date} += $count_per_date; $msc_sequences{$msc_name}->{$date} = $count_per_date; } print join(',', 'MSC_name', @dates) . "\n"; foreach my $msc_name (keys %msc_sequences) { my %tmp = %{$msc_sequences{$msc_name}}; print join(',', $msc_name, @tmp{@dates}) . "\n"; } print join(',', 'Total_cf_produced', @date_totals{@dates}) . "\n";

What I see as part of the problem with your existing program is that you are trying to do too much with one data structure. This leads to tortuous code that's hard to read or make sense of. The fact that this one data structure is called Data, hence giving you no clue as to it's purpose isn't helping.

I tend to find that if I can't think up a meaningfull name for a data structure then I probably need to go back and think about how I'm hanging on to the data. I've also found that in most cases if I have a whole mess of braces in order to get data out of the structure I'm trying too hard.

Struan


In reply to Re: Re: Re: Re: Formating Text file by mr_stru
in thread Formating Text file by redskie007

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.