We help IT Professionals succeed at work.

counting

rw263
rw263 asked
on
foreach $line (@html) {
 ($date, $restOfLine) = split(/\cR/, $line);

 # if $date is the same as the date in the next $line
 # then I want $num to equal the number of lines that $date is the same
 # What should my if() condition be?
 #
 if () {
   print "<tr><td rowspan=$num>$date</td><td>$line</td><//tr>\n"
 }
 else {
   print "<tr><td>$restOfLine</td><//tr>\n";
 }
}
Comment
Watch Question

CERTIFIED EXPERT

Commented:
$l=0; $old='';
foreach $line (@html) {
   ($date, $restOfLine) = split(/\cR/, $line);
   if ($date eq $old) { $lineo=$i; }
   $old=$date;
   $i++;
}

Author

Commented:
ahoffman --

I'm a little confused by your reply. I need to find out if the next line in the array matches the current line we are on. If it does then I need to so how many times that happens. Not really sure where your code should go?
CERTIFIED EXPERT

Commented:
> I need to find out if the next line in the array matches the current

You can't until you read it, the next line, which then becomes the current line and the previous current line becomes the old (or previous) line.
I am a bit confused about what you want.  It seems that each line should be printed in one of two ways, depending on whether the next line has the same date or a different date.
Assume we have the input data:
   date1: Line 1
   date1: Line 2
   date1: Line 3
   date2: Line 4

Should the output be:
   <tr><td rowspan=1>date1</td><td>date1: Line 1</td><//tr>   # Long form because next line has same date
   <tr><td rowspan=2>date1</td><td>date1: Line 2</td><//tr>   # Long form because next line has same date
   <tr><td>Line 3</td><//tr>   # Short form because next line has different date
   <tr><td>Line 4</td><//tr>   # There is no 'Next line' after this one.  Print as if there was another line and it had different date

or perhaps the rowspan shown on the first two output lines should be 3 because there are 3 lines that have date1.

The script below produces an output equivalent to the first option above.

$num = -1;
$oldDate = "?";
foreach $line (@html) {
   chomp $line;
   ($date, $restOfLine) = split(/\cR/, $line);

   # Look for a change in date
   if (($oldDate eq "?") || ($oldDate eq $date)) {
      $num++;
      $different = 0;
   } else {
      $num = 0;
      $different = 1;
   }

   &printOldLine if ($oldDate ne "?");
   $oldLine = $line;
   $oldDate = $date;
   $oldRestOfLine = $restOfLine;
}
&printOldLine;

sub printOldLine {
   # This line has a new date, so print the details of the previous line.
   if (!$different) {
     print "<tr><td rowspan=$num>$oldDate</td><td>$oldLine</td><//tr>\n"
   }
   else {
      print "<tr><td>$oldRestOfLine</td><//tr>\n";
   }
}

Author

Commented:
PC_User321  --

Given:
 date1: Line 1
 date1: Line 2
 date1: Line 3
 date2: Line 4
 date3: Line 5
 date3: Line 6

Output wanted:
<tr><td rowspan=3>date1</td><td>Line 1</td><//tr>   # Long form because next three lines have the same date
<tr><td>Line 2</td><//tr>   # no need for the first cell since the prev line has the same date
<tr><td>Line 3</td><//tr>   # no need for the first cell since the prev line has the same date
<tr><td rowspan=1>date 2</td><td>Line 4</td><//tr>   # Long form because prev line has different date and below line has different date
<tr><td rowspan=2>date1</td><td>Line 5</td><//tr>   # Long form because next two lines have the same date
<tr><td>Line 6</td></tr> #no need for the first cell since the prev line has the same date
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
$num = 0;
$old = '';
@lines = ();
foreach $line ( @html,"\cR" ){
  ($date, $restOfLine) = split(/\cR/, $line);
   if( $date ne $old ){
      print "<tr><td rowspan=$num>$date</td>@lines" if @lines;
      $num = 1;
      @lines = ("<td>$restOfLine</td><//tr>\n");    
  }else{
      push @lines,"<tr><td>$restOfLine</td><//tr>\n";
  }
  $old=$date;
  $num++;
}
Good solution, ozo, except for two mistakes.
(Needs  "print ...._$old_.." and "$num = _0_;")

$num = 0;
$old = '';
@otherLinesSameDate = ();
foreach $line ( @html,"\cR" ){
 ($date, $restOfLine) = split(/\cR/, $line);
 #chomp $restOfLine;
  if( $date ne $old ){
     print "<tr><td rowspan=$num>$old</td>@otherLinesSameDate" if @otherLinesSameDate;
     $num = 0;
     @otherLinesSameDate = ("<td>$restOfLine</td><//tr>\n");    
 }else{
     push @otherLinesSameDate,"<tr><td>$restOfLine</td><//tr>\n";
 }
 $old=$date;
 $num++;
}
I should change "mistakes" to "minor bugs" because ozo got all the important things right.  I like the "\cR" in the 'foreach' line.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.