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

Well I'm actually pulling the data from a database and saved it in the format given in the question then display it on the web, the formatting changed to just MSC_name,count_per_date1...end_date(e.g. 30-JUL-03) for example:

MSC_name,count_per_date1...end_date30(e.g. 30-JUL-03)
MSCBCR1,168,...,170
MSCBCR2,178,...189
Total_cf_produced,346,..,359

The code I'm currently testing is:

#!c:/perl/bin/perl open (INFILE,"c:/playgnd/sql_result_cf.txt") || die("Could not open fi +le!"); while( <INFILE> ) { next if $. == 1; # skip first line chomp; my %F; @F{qw(msc_name chunum date count_per_date)} = split /\s*,\s*/; $Data{$F{date}} = { } unless exists $Data{$F{msc_name}}; $Data{$F{date}}->{$F{msc_name}} = [('')x31] unless ref( $Data{$F{date}}->{$F{msc_name}}); ${$Data{$F{date}}->{$F{msc_name}}}[$F{chunum}] = $F{count_per_date}; } foreach my $date (keys %Data) { foreach my $msc_name (keys %{$Data{$date}}) { print join(",",$msc_name,$date, @{$Data{$date}->{$msc_name}}), "\ +n" ; } } close(INFILE);
Thanks.

Replies are listed 'Best First'.
Re: Re: Re: Re: Formating Text file
by mr_stru (Sexton) on Aug 19, 2003 at 06:29 UTC

    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

      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