For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2006 > Problem with file hashes









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Problem with file hashes
Mike

2006-10-19, 6:56 pm

I'm having a little bit of problems finishing up a previous co-workers
perl project. I'm a little as to why its not working. What
the script is doing is add values in a text file together (daily
values) and add up the totals and throw it in a monthly total. I think
there is something going on with the hashes, or entering the data into
the hashes.

#!/bin/perl
@servers = ("server1", "server2");
%values1 = ();
foreach $server (@servers){
print "$server\n";
opendir DATADIR, "$server";
@files = readdir DATADIR;
closedir DATADIR;
foreach $file (@files){
chomp;
next if ($file eq "." || $file eq "..");
open(IN," < $server/$file") or next;
while(<IN> ){
chomp;
next if (length $_ == 0);
next if ($_ =~ /--------|Report Title/);
if ($_ =~ /This is Header 1|This is Header 2|This is Header 3/) {
$prefix = $_;
}
if ($_ =~ /Sub Report 1-1\D+:|Sub Report 2-1\D+:|number\D+:|Sub
Report 3-1\D+:)\s+(\d+)/) {
$suffix = $1;
$key = $prefix . $suffix;
$value = $2;
if (exists $values1{$key}) {
$values1{$key} += $value;
}else{
$values1{$key} = $value;
}
}
}
}
}
open (FILEOUT, ">data.txt");
while ( ($key,$value) = each %values1 ) {
print "$key => $value\n";
}
foreach $key (keys (%values))
{
print FILEOUT $key, ",", $values1{$key}, "\n";
}
__END__


Variable names are at begining of each line. Sample Report follows.

---------------------------------------------------------------------------
Report Title
---------------------------------------------------------------------------

Start date: 20060202
End date: 20060203

This is header 1:

Sub Report 1-1: 2504
Sub Report 1-2: 16320
Sub Report 1-3: 83
Sub Report 1-4: 63

This is header 2:

Sub Report 2-1: 785
Sub Report 2-2: 363
Sub Report 2-3: 725
Sub Report 2-4: 9835

This is header 3:

Sub Report 3-1: 9653
Sub Report 3-2: 135
Sub Report 3-3: 132
Sub Report 3-4: 135

kens

2006-10-19, 6:56 pm


Mike wrote:
> I'm having a little bit of problems finishing up a previous co-workers
> perl project. I'm a little as to why its not working. What
> the script is doing is add values in a text file together (daily
> values) and add up the totals and throw it in a monthly total. I think
> there is something going on with the hashes, or entering the data into
> the hashes.
>
> #!/bin/perl
> @servers = ("server1", "server2");
> %values1 = ();
> foreach $server (@servers){
> print "$server\n";
> opendir DATADIR, "$server";
> @files = readdir DATADIR;
> closedir DATADIR;
> foreach $file (@files){
> chomp;
> next if ($file eq "." || $file eq "..");
> open(IN," < $server/$file") or next;
> while(<IN> ){
> chomp;
> next if (length $_ == 0);
> next if ($_ =~ /--------|Report Title/);
> if ($_ =~ /This is Header 1|This is Header 2|This is Header 3/) {
> $prefix = $_;
> }
> if ($_ =~ /Sub Report 1-1\D+:|Sub Report 2-1\D+:|number\D+:|Sub
> Report 3-1\D+:)\s+(\d+)/) {



Does this compile? I see unmatched parens.


> $suffix = $1;
> $key = $prefix . $suffix;
> $value = $2;
> if (exists $values1{$key}) {
> $values1{$key} += $value;
> }else{
> $values1{$key} = $value;
> }
> }
> }
> }
> }
> open (FILEOUT, ">data.txt");
> while ( ($key,$value) = each %values1 ) {
> print "$key => $value\n";
> }
> foreach $key (keys (%values))
> {
> print FILEOUT $key, ",", $values1{$key}, "\n";
> }
> __END__
>
>
> Variable names are at begining of each line. Sample Report follows.
>
> ---------------------------------------------------------------------------
> Report Title
> ---------------------------------------------------------------------------
>
> Start date: 20060202
> End date: 20060203
>
> This is header 1:
>
> Sub Report 1-1: 2504
> Sub Report 1-2: 16320
> Sub Report 1-3: 83
> Sub Report 1-4: 63
>
> This is header 2:
>
> Sub Report 2-1: 785
> Sub Report 2-2: 363
> Sub Report 2-3: 725
> Sub Report 2-4: 9835
>
> This is header 3:
>
> Sub Report 3-1: 9653
> Sub Report 3-2: 135
> Sub Report 3-3: 132
> Sub Report 3-4: 135


Why don't you get some sample input and see what results you get. Maybe
someone can determine something by looking at the code, but without
knowing what's wrong (you are not very specific about that), I'm not
going to spend much time on it.

Ken

kens

2006-10-19, 6:56 pm


kens wrote:
> Mike wrote:
>
>
> Does this compile? I see unmatched parens.
>
>
>
> Why don't you get some sample input and see what results you get. Maybe
> someone can determine something by looking at the code, but without
> knowing what's wrong (you are not very specific about that), I'm not
> going to spend much time on it.
>
> Ken


One other thing, put

use strict;
use warnings;

at the top of your script and declare your variables before using them.
That might put out things like your use of %values in the following
line:

foreach $key (keys (%values))

where did %values come from?

Ken

kens

2006-10-19, 6:56 pm


Mike wrote:
> I'm having a little bit of problems finishing up a previous co-workers
> perl project. I'm a little as to why its not working. What
> the script is doing is add values in a text file together (daily
> values) and add up the totals and throw it in a monthly total. I think
> there is something going on with the hashes, or entering the data into
> the hashes.
>
> #!/bin/perl
> @servers = ("server1", "server2");
> %values1 = ();
> foreach $server (@servers){
> print "$server\n";
> opendir DATADIR, "$server";
> @files = readdir DATADIR;
> closedir DATADIR;
> foreach $file (@files){
> chomp;
> next if ($file eq "." || $file eq "..");
> open(IN," < $server/$file") or next;
> while(<IN> ){
> chomp;
> next if (length $_ == 0);
> next if ($_ =~ /--------|Report Title/);
> if ($_ =~ /This is Header 1|This is Header 2|This is Header 3/) {
> $prefix = $_;
> }
> if ($_ =~ /Sub Report 1-1\D+:|Sub Report 2-1\D+:|number\D+:|Sub
> Report 3-1\D+:)\s+(\d+)/) {
> $suffix = $1;
> $key = $prefix . $suffix;
> $value = $2;
> if (exists $values1{$key}) {
> $values1{$key} += $value;
> }else{
> $values1{$key} = $value;
> }
> }
> }
> }
> }
> open (FILEOUT, ">data.txt");
> while ( ($key,$value) = each %values1 ) {
> print "$key => $value\n";
> }
> foreach $key (keys (%values))
> {
> print FILEOUT $key, ",", $values1{$key}, "\n";
> }
> __END__
>
>
> Variable names are at begining of each line. Sample Report follows.
>
> ---------------------------------------------------------------------------
> Report Title
> ---------------------------------------------------------------------------
>
> Start date: 20060202
> End date: 20060203
>
> This is header 1:
>
> Sub Report 1-1: 2504
> Sub Report 1-2: 16320
> Sub Report 1-3: 83
> Sub Report 1-4: 63
>
> This is header 2:
>
> Sub Report 2-1: 785
> Sub Report 2-2: 363
> Sub Report 2-3: 725
> Sub Report 2-4: 9835
>
> This is header 3:
>
> Sub Report 3-1: 9653
> Sub Report 3-2: 135
> Sub Report 3-3: 132
> Sub Report 3-4: 135



Do not multipost. You have wasted my time as I responded earler when
others
had already responded in comp,lang.perl.misc.

You should always post in the one best newsgroup. But sometimes an
article genuinely fits the topic of multiple groups very well. For
instance, if you've had an especially good or bad experience with an
online vendor of DVDs, it might be appropriate to post it to
misc.consumers and alt.video.dvd. But when this is appropriate, you
want to crosspost, not multipost.

To crosspost, you put both newsgroup names on the Newsgroups line of
your article. (The way to do this depends on your software: consult
your help file under "Crossposting".) Crossposting sends out only
one copy of the article, marked so that it shows up in both newsgroups.
With most newsreader software, anyone who looks at either newsgroup
will see it, but people who happen to read both newsgroups will see it
only once.

Mike

2006-10-19, 9:56 pm


kens wrote:
> Mike wrote:
>
>
> Do not multipost. You have wasted my time as I responded earler when
> others
> had already responded in comp,lang.perl.misc.
>
> You should always post in the one best newsgroup. But sometimes an
> article genuinely fits the topic of multiple groups very well. For
> instance, if you've had an especially good or bad experience with an
> online vendor of DVDs, it might be appropriate to post it to
> misc.consumers and alt.video.dvd. But when this is appropriate, you
> want to crosspost, not multipost.
>
> To crosspost, you put both newsgroup names on the Newsgroups line of
> your article. (The way to do this depends on your software: consult
> your help file under "Crossposting".) Crossposting sends out only
> one copy of the article, marked so that it shows up in both newsgroups.
> With most newsreader software, anyone who looks at either newsgroup
> will see it, but people who happen to read both newsgroups will see it
> only once.


Sorry about the multi-post, I'm still trying to get familiar with
Google groups. Again, please accept my apologies, and thanks for the
help.

I will be reviewing all the feedback I received, and I will go over all
this tomorrow.

Thanks.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com