in reply to Formating Text file

my code was:
#!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}} = [('')x5] unless ref( $Data{$F{date}}->{$F{msc_name}}); ${$Data{$F{date}}->{$F{msc_name}}}[$F{hour}] = $F{count_per_hour}; } foreach my $date (keys %Data) { foreach my $msc_name (keys %{$Data{$date}}) { print join(",", $date, $msc_name, @{$Data{$date}->{$msc_name}}), +"\n" ; } } close(INFILE);

edited by ybiC: balanced <code> tags

Replies are listed 'Best First'.
Re: Re: Formating Text file
by mr_stru (Sexton) on Aug 15, 2003 at 09:03 UTC

    Hi,

    You might want to consider providing more and better formatted information here. I've looked at this, tried to run your code and failed but because I really can't work out exactly what you are trying to do it's really hard to try and fix it.

    Starting with a smaller example of the input data, a corresponding example of what you are trying to output and possibly some comments in your code would go a long way to making it all a lot clearer for people wanting to help you.

    Struan

      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.

        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