in reply to Re: Re: Re: Formating Text file
in thread Formating Text file

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

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Formating Text file
by redskie007 (Initiate) on Aug 19, 2003 at 08:21 UTC
    Thanks for the help.. I tried out your code and it worked well but I can't understand why it repeat the dates, maybe because of how push behaves?

      Doh!

      Yes, I didn't really think that through carefully enough. What I should have said was:

      push @dates, $date unless defined $date_totals{$date};

      Struan