Re: how to sort certain pattern from a file

2004-06-22 Thread James Edward Gray II
On Jun 16, 2004, at 2:25 PM, Naser Ali wrote:
Thanks James,
I totally agree with you and appreciate your comments.
I was going to refine the whole code by putting in better logic, naming
convention, and error handling. I just posted the code baically to 
share the
basic logic of handling the situation. I also did not use the correct
wording as you pointed out that "$#array" is the last index not the 
size. I
ll be more carefull with the wording as well to show my understanding 
of the
matter.
I ll definitely be more careful next time. You are right it is good to 
get
in this habbit.

Much appreciated your comments and guidance.
I meant to do this earlier, but my wife wisked me away on a surprise 
vacation for my birthday.  ;)  I'm sure you've about forgot this code 
by now, but...

Just for comparison, I wanted you too see how I might go about writing 
the script you did (since I just gave general tips last time).

The code below should do something similar to what you posted.  Maybe 
it'll give you some ideas.

James
#!/usr/bin/perl
use strict;
use warnings;
while (<>) {  # call with perl script_name input file name
print if m/Products/..m/System Totals/;
}
__END__
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



RE: how to sort certain pattern from a file

2004-06-16 Thread Naser Ali
Thanks James,

I totally agree with you and appreciate your comments.
I was going to refine the whole code by putting in better logic, naming
convention, and error handling. I just posted the code baically to share the
basic logic of handling the situation. I also did not use the correct
wording as you pointed out that "$#array" is the last index not the size. I
ll be more carefull with the wording as well to show my understanding of the
matter.
I ll definitely be more careful next time. You are right it is good to get
in this habbit.

Much appreciated your comments and guidance.

Thanks


-Original Message-
From: James Edward Gray II [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, June 16, 2004 2:02 PM
To: Naser Ali
Cc: [EMAIL PROTECTED]
Subject: Re: how to sort certain pattern from a file


On Jun 16, 2004, at 12:22 PM, Naser Ali wrote:

> Hello All,

Hello.

> 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.

Your code is a good start, but there are things that could be better.  
I'm going to make some general comments below.  Then, if you like, you 
can make some changes and repost and I promise to look again...

> #!/usr/bin/perl

Two lines missing right here:

use strict;
use warnings;

These promise you will obey The Rules of Good Programming and Perl with 
pay you back with meaningful messages that help you do your job.  It's 
a great deal.

Your code won't currently compile with these in.  You'll need some 
changes.  Mainly, you need to declare variables before you use them (or 
when you initialize them).  Use my() for this.  Example:

my $count = 0;

> $want=1;
> $count=0;

I seriously doubt these are needed and certainly not way up here were 
they tell me nothing.

> open(STDIN, "test.txt");

First, if we ever ask the OS to do work for us, we need to be sure it 
succeeds.  It could fail for many, many reasons and if it does, we want 
to know why.

Second, let STDIN do what STDIN is suppose to do.  Use your own file 
handle.

Putting that together, we get:

open REPORT, 'test.txt' or die "File error:  $!";

> @array=;

An @ called array is redundant and tells me nothing when I'm trying to 
read code.  Use meaningful names.

my @lines = ;

> close (STDIN);

close REPORT;

> print "Size of the Array is $#array\n";

Wrong.  $#array is the last index, not the size.

print "The array contains ", scalar(@array), " members\n",
"The index of the last member is $#array.\n;

> print "@array\n";
>
> for ($x=0; $x <= $#array; $x++) {
>
> print "Index[$x] > $array[$x]\n";
> }

Yuck.  That looks like C.  ;)  We let are language handle those 
annoying details for us:

for my $i (0..$#array) {
print "...";
}

# or, if you don't need the index...

print "$_\n" for @array;

> 
>
> $k=0;

Again, meaningful names.  What does this variable track, line number?  
Tell me that.

Do we need to track line numbers?  No.  They're in an array so the line 
they were on is index + 1.  We already have that info.  Looks line you 
are even tracking indexes, not line numbers, so we don't even need the 
+ 1.

> foreach $line (@array){
>
> chomp $line;
>
> if ($line =~ /Product/g)  {

We don't need a global modifier on our match, we're just checking if 
it's in there.

>if (++$count == $want) {

Are we just trying to make sure we only get the first one?  See my next 
comment...

>  print "line number is ---> $k\n";
>  $FIRST=$k;

last; # end loop, we'll only get the first one

>}
> }
> $k++;
> }

for my $i (0..$#array) {
if ($array[$i] =~ m/Product/) { $FIRST = $i; }
elsif ($array[$i] =~ m/System Totals/) { $LAST = $i; }
}

> $i=0;
> $want=1;
> $count=0;

I doubt we need any of these.  See comments above.

> foreach $line (@array){
>
> chomp $line;
>
> if ($line =~ /System Totals/g)  {
>if (++$count == $want) {
>  print "line number is ---> $i\n";
>  $LAST=$i;
>}
> }
> $i++;
> }

Just like the previous loop.

> $j=0;
>
> for ($ii=$FIRST; $ii <= $LAST; $ii++) {
>
> $array2[$j] = $array[$ii];
> $j++;
> }

What are we doing here?  Making another array of first to las

RE: how to sort certain pattern from a file

2004-06-16 Thread Wiggins d Anconia
> 
> 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  TodayYesterday   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.001842.242512.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.832147.752631.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.792509.92
>  
>  
>  
> 
> Insts   2502

Re: how to sort certain pattern from a file

2004-06-16 Thread James Edward Gray II
On Jun 16, 2004, at 12:22 PM, Naser Ali wrote:
Hello All,
Hello.
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.
Your code is a good start, but there are things that could be better.  
I'm going to make some general comments below.  Then, if you like, you 
can make some changes and repost and I promise to look again...

#!/usr/bin/perl
Two lines missing right here:
use strict;
use warnings;
These promise you will obey The Rules of Good Programming and Perl with 
pay you back with meaningful messages that help you do your job.  It's 
a great deal.

Your code won't currently compile with these in.  You'll need some 
changes.  Mainly, you need to declare variables before you use them (or 
when you initialize them).  Use my() for this.  Example:

my $count = 0;
$want=1;
$count=0;
I seriously doubt these are needed and certainly not way up here were 
they tell me nothing.

open(STDIN, "test.txt");
First, if we ever ask the OS to do work for us, we need to be sure it 
succeeds.  It could fail for many, many reasons and if it does, we want 
to know why.

Second, let STDIN do what STDIN is suppose to do.  Use your own file 
handle.

Putting that together, we get:
open REPORT, 'test.txt' or die "File error:  $!";
@array=;
An @ called array is redundant and tells me nothing when I'm trying to 
read code.  Use meaningful names.

my @lines = ;
close (STDIN);
close REPORT;
print "Size of the Array is $#array\n";
Wrong.  $#array is the last index, not the size.
print "The array contains ", scalar(@array), " members\n",
"The index of the last member is $#array.\n;
print "@array\n";
for ($x=0; $x <= $#array; $x++) {
print "Index[$x] > $array[$x]\n";
}
Yuck.  That looks like C.  ;)  We let are language handle those 
annoying details for us:

for my $i (0..$#array) {
print "...";
}
# or, if you don't need the index...
print "$_\n" for @array;

$k=0;
Again, meaningful names.  What does this variable track, line number?  
Tell me that.

Do we need to track line numbers?  No.  They're in an array so the line 
they were on is index + 1.  We already have that info.  Looks line you 
are even tracking indexes, not line numbers, so we don't even need the 
+ 1.

foreach $line (@array){
chomp $line;
if ($line =~ /Product/g)  {
We don't need a global modifier on our match, we're just checking if 
it's in there.

   if (++$count == $want) {
Are we just trying to make sure we only get the first one?  See my next 
comment...

 print "line number is ---> $k\n";
 $FIRST=$k;
last; # end loop, we'll only get the first one
   }
}
$k++;
}
for my $i (0..$#array) {
if ($array[$i] =~ m/Product/) { $FIRST = $i; }
elsif ($array[$i] =~ m/System Totals/) { $LAST = $i; }
}
$i=0;
$want=1;
$count=0;
I doubt we need any of these.  See comments above.
foreach $line (@array){
chomp $line;
if ($line =~ /System Totals/g)  {
   if (++$count == $want) {
 print "line number is ---> $i\n";
 $LAST=$i;
   }
}
$i++;
}
Just like the previous loop.
$j=0;
for ($ii=$FIRST; $ii <= $LAST; $ii++) {
$array2[$j] = $array[$ii];
$j++;
}
What are we doing here?  Making another array of first to last?
my @first_to_last = @array[$FIRST..$LAST];
for ($y=0; $y <= $#array2; $y++) {
print "Index[$y] > $array2[$y]\n";
}
print "FIRST--> $FIRST   and LAST--->$LAST\n";
Let me comment on one last issue.  You're "slurping" the file or 
reading the whole thing into memory in one move.  Then you have to do a 
dance, to get what you want.  Too much hassle.

The right way is to only read in what you want and deal with that.  You 
handle this line by line as it's coming in. See if you can setup 
something like that.

Good luck and yell if you need help...
James
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



RE: how to sort certain pattern from a file

2004-06-16 Thread Naser Ali
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

--

#!/usr/bin/perl

$want=1;
$count=0;

open(STDIN, "test.txt");
@array=;
close (STDIN);

print "Size of the Array is $#array\n";
print "@array\n";

for ($x=0; $x <= $#array; $x++) {

print "Index[$x] > $array[$x]\n";
}


$k=0;
foreach $line (@array){

chomp $line;

if ($line =~ /Product/g)  {
   if (++$count == $want) {
 print "line number is ---> $k\n";
 $FIRST=$k;
   }
}
$k++;
}

$i=0;
$want=1;
$count=0;

foreach $line (@array){

chomp $line;

if ($line =~ /System Totals/g)  {
   if (++$count == $want) {
 print "line number is ---> $i\n";
 $LAST=$i;
   }
}
$i++;
}

$j=0;

for ($ii=$FIRST; $ii <= $LAST; $ii++) {

$array2[$j] = $array[$ii];
$j++;
}



for ($y=0; $y <= $#array2; $y++) {

print "Index[$y] > $array2[$y]\n";
}

print "FIRST--> $FIRST   and LAST--->$LAST\n";
- 

-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  TodayYesterday   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.001842.242512.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.832147.752631.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.792509.92
 
 
 

Insts   250250250   7500  62644  93496
   Occ. Insts   250250250   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.392877.72   21922.86   33578.77
 

Reports menu?r
 
 
 
 Instance Month  Last   Year   Last
  name  TodayYesterday   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.101760.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.002058.00 342.00
Task 3
Avg/Inst  0.03   0.00   0.00   0.04   0.03   0.03
 
.
.
.
.
.
.
.
.
 

===


how to sort certain pattern from a file

2004-06-15 Thread Naser Ali
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  TodayYesterday   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.001842.242512.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.832147.752631.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.792509.92
 
 
 

Insts   250250250   7500  62644  93496
   Occ. Insts   250250250   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.392877.72   21922.86   33578.77
 

Reports menu?r
 
 
 
 Instance Month  Last   Year   Last
  name  TodayYesterday   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.101760.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.002058.00 342.00
Task 3
Avg/Inst  0.03   0.00   0.00   0.04   0.03   0.03
 
.
.
.
.
.
.
.
.
 

===