in reply to Consolidate info in a while() loop

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

Replies are listed 'Best First'.
Re: Re: Consolidate info in a while() loop
by peppiv (Curate) on Jan 21, 2003 at 21:41 UTC
    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
        You!........You my friend are very good!

        No, I'm not!

        Yes, you are!

        No, no.

        Hey, I said you was good!

        Analyze that!

        peppiv

        Works like a charm! Had to declare %total_day and take out the formatting of the number before it was crunched, but hey, you is good!

      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