1. When you post code surround it by <code> and </code>. This way the indentation and square brackets will not get lost.

2. Always use the "-w" option and "use strict;" in your programs. This will help identify bugs. The "-T" option is also fun to add to keep yourself honest. If/when you get warnings about the use of tainted data then you sit down and think about the security implications of your program.

3. In your code you create a data structure which has an array of five elemnts which are indexed by a field called "hour". (a) I find it strange that "hour" would only have five values. (b) There is no field called "hour" in the input you parse, nor does the data itself seem to have anything resembling an hour.

4. There seems to be an extra dereferencing on the last line of the first loop.

5. The print statement does not seem to match the number/types of fields you describe should be output (though it's not easy for me to understand from the description exactly what is desired).

Here is your program with a few tweaks. It generates output, but has the warnings about uninitialized values caused by the fact that there is no "hour" input field.

#!c:/perl/bin/perl -wT use strict; my %Data; open (INFILE,"c:/playgnd/sql_result_cf.txt") || die("Could not open file!"); my $header = <INFILE>; while( <INFILE> ) { 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);

This obviously does not do what you want, but it runs and is perhaps it is further in the right direction.

-- Eric Hammond


In reply to Re: Formating Text file by esh
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.