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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.