Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Consolidate info in a while() loop

by peppiv (Curate)
on Jan 21, 2003 at 19:25 UTC ( #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
Node Status?
node history
Node Type: perlquestion [id://228798]
Approved by BrowserUk
help
Chatterbox?
[Discipulus]: good morning Corion!
[Corion]: I spent parts of yesterday digging into Filter::Simple to make it not change the line numbers of unfiltered code, but I think I need regex help (which will come in a SoPW I guess). I tried @lines = /((?:"...."|[^" \n]+)\n)/gs, but that matched on...
[Corion]: ... foo "\0\0\0\n" bar\n by matching the first \n instead of matching the four-bytes-in- double-quotes part
[Corion]: ... and I still don't understand why ;)
[Corion]: This is because Filter::Simple does some string-trickery, replacing all string literals with "quoted packed numbers"
[Corion]: I fear this might be a bug in the RE engine, but if it is a bug, even fixing won't help me because I need Filter::Simple for Filter::signatures , which provides signatures as a backwards compatibility feature for Perl <5.22 - and these ...
[Corion]: ... won't get a fix anyway ;) My plan B is to encode the string placeholders avoiding \r and \n

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (10)
As of 2017-01-23 08:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you watch meteor showers?




    Results (191 votes). Check out past polls.