> 
> Hello All,
> 
> Yesterday I posted a question asking if anyone can suggest a way of
> accomplishing this. In the mean while I have comeup with a quick and dirty
> way of processing the data file and sorting it in an array. Once it is
> sorted, then, I can do whatever however I need to. Code is attached below.
> If you have any suggestions on better way of accomplishing the same task,
> please share.
> 
> Thanks
> 

See James' comments as they looked spot on with respect to the code you
had.  I was going to write something similar and then just decided to
take a stab at the bigger picture.  If your report is as sane as I think
it is have a look at this,

use strict;
use warnings;

my $instances = {};
my $instance = 0;

my @keys =
('desc','today','yesterday','month_to_date','last_month','year_to_date','last_year');

my $REPORT;
open $REPORT, 'report.txt' or die "Can't open report: $!";
while (my $line = <$REPORT>) {
    if ($line =~ /Instance/) {
        ++$instance;
        <$REPORT>; # skip line
        <$REPORT>; # skip line

        while (my $line2 = <$REPORT>) {
            if ($line2 =~ /Task (\d+)/) {
                my $task = $1;
                foreach my $key ('avg_inst', 'avg_occ_inst',
'total_count') {
                    $instances->{$instance}->{$task}->{$key} = <$REPORT>;
                }
            }
            elsif ($line2 =~ /Insts/) {
                foreach my $key ('insts', 'occ_insts',
'avg_inst','avg_occ_inst','system_totals') {
                    $instances->{$instance}->{'summary'}->{$key} =
<$REPORT>;
                }
                last;
            }
        }
    }
}
close $REPORT;

use Data::Dumper;
print Dumper($instances);

End up with a data structure that should suit your original request. I
didn't feel like dealing with the individual pieces of a data line since
it looks like fixed length which generally bores me, but it should be
trivial to add parsing such that the individual values are stored back
to the data structure as well.

If you need to go the file+array route then you might want to consider
Tie::File.

http://danconia.org


> 
> -----Original Message-----
> From: Naser Ali [mailto:[EMAIL PROTECTED] 
> Sent: Tuesday, June 15, 2004 4:22 PM
> To: [EMAIL PROTECTED]
> Subject: how to sort certain pattern from a file
> 
> 
> Below is a sample report. I have been able to only capture lines between
> "Intance Name" and "System totals", but here is my delima.
>  
> The menus on the report are repeated several times. For example, the line
> starts with "Instance Name" appears twice. I only want to capture the
lines
> between first occurence of "Instance Name" and upto "System Totals" ,
> process it or copy in another temp file and then delete it from the
original
> file, so I can process the next one and so on and so forth. Also, as
you can
> see that each "Task" has three lines underneath it with related data, how
> can I process it and associate it in arrays or lists.What is the best of
> doing it? For instance, for "Task 1", I would like to capture lines
starting
> with "Avg/Inst", "Avg/Occ.Inst", and " Total Count" be stored in an
array or
> a file, which ever is best to process later, but keep the association
> maintained.
> 
> I ll truly appreciate any help and pointers.
> Thanks 
>  
>  
>
============================================================================
> ===============
>  
>  Instance                         Month      Last       Year       Last
>   name      Today    Yesterday   to Date     Month     to Date     Year
>    --------  ---------  ---------  ---------  ---------  ---------
> ---------
> Task 1
>     Avg/Inst  0.03       0.00       0.00       0.04       0.03       0.03
> Avg/Occ.Inst  0.03       0.00       0.00       0.04       0.03       0.03
>  Total Count  8.08       0.00       8.08     303.00    1842.24    2512.88
> Task 2
>     Avg/Inst  0.03       0.00       0.00       0.04       0.03       0.03
> Avg/Occ.Inst  0.03       0.00       0.00       0.04       0.03       0.03
>  Total Count  7.81       0.00       7.81     335.83    2147.75    2631.97
> Task 3
>     Avg/Inst  0.00       0.03       0.03       0.01       0.02       0.03
> Avg/Occ.Inst  0.00       0.03       0.03       0.01       0.02       0.03
>  Total Count  0.00       7.56       7.56     109.62     965.79    2509.92
>  
>  
>  
> 
>         Insts   250        250        250       7500      62644      93496
>    Occ. Insts   250        250        250       7500      62625      93496
>      Avg/Inst  0.15       0.18       0.33       0.38       0.35       0.36
>  Avg/Occ.Inst  0.15       0.18       0.33       0.38       0.35       0.36
> System Totals 36.36      45.03      81.39    2877.72   21922.86   33578.77
>  
> 
> Reports menu?r
>  
>  
>  
>  Instance                         Month      Last       Year       Last
>   name      Today    Yesterday   to Date     Month     to Date     Year
> --------  ---------  ---------  ---------  ---------  ---------  ---------
> Task 1
>     Avg/Inst  0.00       0.02       0.02       0.01       0.01       0.02
> Avg/Occ.Inst  0.00       0.02       0.02       0.01       0.01       0.02
>  Total Count  0.00       4.85       4.85     101.85     902.10    1760.55
> Task 2
>     Avg/Inst  0.00       0.02       0.02       0.03       0.03       0.02
> Avg/Occ.Inst  0.00       0.02       0.02       0.03       0.03       0.02
>  Total Count  0.00       6.00       6.00     228.00    2058.00     342.00
> Task 3
>     Avg/Inst  0.03       0.00       0.00       0.04       0.03       0.03
>  
> .....................
> .....................
> .....................
> .....................
> .....................
> .....................
> .....................
> .....................
>  
>
============================================================================
> ===============
> 
> 



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to