Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

You may be looking for a Perl solution, so I'll give you feedback. You may be learning Perl, or may not be running on a Unix platform.

First a comment about lexical variables and use strict;. Get yourself into the habit of declaring your variables with my, and only declaring them in the narrowest scope that you need them. A Super search on "use strict" will give you many answers that explain why this is a good idea.

Secondly, your test for evenness of the line count is the wrong way to do things. Each time you get an odd number of singular lines, it will throw out the evenness check.

Here's my stab:

use strict; use warnings; my $prev; while (<DATA>) { chomp; # Note that $prev will be undef only on the first time round. my $curr = $_; if (!defined $prev) { $prev = $curr; next; } if ($curr ne $prev) { print "$prev\n"; $prev = $curr; } else { undef $prev; } } print "$prev\n" if defined $prev; __DATA__ a1a a1a b1b c1c c1c d1d d1d e1e f1f g1g g1g h1h h1h i1i j1j

Note that you don't deal with, or say whay you want to happen when you get three or more identical lines in the input.

--
Apprentice wetware hacker


In reply to Re: Read file line by line and check equal lines by rinceWind
in thread Read file line by line and check equal lines by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-04-19 11:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found