Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Consolidate info in a while() loop

by peppiv (Curate)
on Jan 21, 2003 at 19:25 UTC ( [id://228798]=perlquestion: print w/replies, xml ) Need Help??

peppiv has asked for the wisdom of the Perl Monks concerning the following question:

SWM - non-smoker, desperately seeks...........Oops! wrong text.

I've got a problem consolidating information that is picked up from a while loop. Let's take a look........

my $count3 = 0; my $cgi3 = "$urchinpath/bin/urchin.cgi"; $ENV{REQUEST_METHOD} = "GET"; if (defined $field{$report3}) { $ENV{QUERY_STRING} = "r=$report3&b=$begin&e=$end&v=$max&rid=$rid&la +ng=$language&f=$field{$report3}"; } else { $ENV{QUERY_STRING} = "r=$report3&b=$begin&e=$end&v=$max&rid=$rid&la +ng=$language&f=$tab"; } open (CGI, "$cgi3 |") or die "Failed to open urchin.cgi: $!"; while(<CGI>) { if ($_ =~ /^parent.dLoad\((.*)\);?\s*/) { @line = split(/,/, $1); if (($report3 != 1102) && ($report3 != 1901) && ($report3 != 190 +2) && ($report3 != 1905)) { $line[0] = (split(/"/, $line[0]))[1]; } if (!defined $line[0]) { $line[0] = ""; } $line[1] = &formatNumber($line[1]); if (($report3 == 1101)) { printf("%-66s %13s\n", $line[0], $line[1]); #Here's the bugger. This is the line that prints what I need +. } elsif (defined $field{$report3}) { printf("%-66s %13s\n", $line[0], $line[($field{$report3}+1)]) +; } $count3++; } } close(CGI) or die "Failed to close urchin.cgi: $!"; exit;

This produces results that look like this:

Wed 1/01     1,342
Thu 1/02     1,876
Fri 1/03     2,467
Sat 1/04     1,567
Sun 1/05     3,899
Mon 1/06     2,544
Tue 1/07     1,235
Wed 1/08     2,099

What I need to do is format it like this:
Mon 2,544
Tue 1,235
Wed 3,441

Where I only list the day of the week once, but the numbers for this day is totaled. I'm trying to put this into a DB and it'd be a lot easier to put it in by day of month as opposed to by the day and have to create all these extra rows.

Too burnt to figure this out and need help! Anyone have any ideas?

peppiv

Replies are listed 'Best First'.
Re: Consolidate info in a while() loop
by CountZero (Bishop) on Jan 21, 2003 at 19:38 UTC

    Make a hash where the names of the weekdays are the keys and the numbers are the values.

    Then do something like $total{$weekday} += $number;

    Do this at the spot where you previously printed the numbers and print after the while-loop has run its course.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      I understand the concept but I have no idea how to start this. How do you build a hash from a while loop?

      peppiv

        Example
        if (!defined $line[0]) { $line[0] = ""; } # $line[1] = &formatNumber($line[1]); ### take this out if (($report3 == 1101)) { printf("%-66s %13s\n", $line[0], $line[1]); #Here's the bugger. This is the line that prints what I need my $weekday = substr($line[0],0,3); ## add $total_day{$weekday} += $line[1]; ## add +. } elsif (defined $field{$report3}) { printf("%-66s %13s\n", $line[0], $line[($field{$report3}+1)]) +; } $count3++; } } # print totals in correct order my @days = qw(Mon Tue Wed Thu Fri Sat Sun); for (@days){ printf( "%-66s %13s\n",$_,formatNumber($total_day{$_}) ); }
        poj

        In your while-loop, obtain both the day of the week without the date (to be put into a variable $weekday) and the number (put this into $number).

        Once you have these data, then $total{$weekday} += $number; will add $number to the value of the hash %total keyed by $weekday.

        That's all there is to it.

        CountZero

        "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://228798]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-23 09:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found