in reply to Re^5: counting lines in perl
in thread counting lines in perl

Hmmm... let's see if I can explain this clearly. Basically, the code you originally posted spit out data as soon as it encountered a unique line. That is, it kept track of the previous line and as soon as the current line was different from the previous one (unless($line eq $oldline)), it would print it. The problem with printing the new unique line immediately is that you cannot count how often it appears in sequence, because you don't know yet. You've only found the first occurance of it at that point.

The solution then is to print each unique line after its last appearance in a sequence, not the first. To answer your question, that's what the "else" part within the loop does. It does not stop the loop (which just iterates over all of your input lines, after all).

The else executes if the current line is different from the one that's being kept track of (the $oldline). In other words, it executes as soon as you encounter a new unique line. The code prints the line along with the count, and then resets the counter (the $n = 1; line).

That last if statement will actually always evaluate to true. It says: "If there is any text in $oldline, then print it along with its count. If we didn't do this, the very last unique line would never get printed.

To pseudo-code it:
Read in my first input line and store it in $oldline Now loop over the rest of the input lines if the current line is the same as $oldline, then increment our count otherwise (that is, the current line is different) print $oldline and the counter reset the counter set the current line as the next line to keep track of by storin +g it in $oldline Loop if there's something in $oldline (which will always be the case), prin +t it

It just struck me... I hope I haven't just done your homework assignment or something.

Update: I see it was homework, but in light of imhotep's explanation and the disclaimers on previous nodes, I don't mind having done it. I miss college anyway.

Replies are listed 'Best First'.
Re^7: counting lines in perl
by imhotep (Novice) on Feb 26, 2005 at 23:57 UTC
    Thanks crashtest. In answer to your last comment, yes this is an assignment for university, but you may be able to tell I really want to understand the code and not merely get somebody else to do the work and then hand it in, hence asking for an explanation. The aim of any course should ultimately be to understand better the subject, I think the awarding of marks (or not) is not always a good indicator of this process, but failure often is an indicator of a lack of understanding. I did not mean to decieve you, if you are dissapproving I sincerely apologise. Thanks again!