Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: to avoid redundacy in a file

by amphiplex (Monk)
on Jul 15, 2002 at 05:31 UTC ( [id://181722]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to to avoid redundacy in a file

You could remove all duplicate lines with something like this:
my %seen; while (<>) { next if $seen{$_}; print; $seen{$_}++; }

---- amphiplex

Replies are listed 'Best First'.
Re^2: to avoid redundacy in a file
by tadman (Prior) on Jul 15, 2002 at 05:43 UTC
    To avoid redundancy in your code, you could do this:
    my %seen; while (<>) { next if ($seen{$_}++); print; }
    You can do it in one shot, so you might as well. Note that this code eliminates all duplicate lines, not just repeated ones. If you want to just ditch repeats, use this:
    my $last; while (<>) { next if ($_ eq $last); $last = $_; print; }
    Thus lines "A A A B B B A A C C" will be "A B A C" not "A B C" as in the previous bit.
      You can do it in one shot, so you might as well.
      That makes your second snippet
      my $prev; while (<>) { next if ($_ eq $prev); print $prev = $_; }
      :^)

      Wait, we can shorten that..
      my $prev; while (<>) { print $prev = $_ unless $_ eq $prev; }
      Hmm..
      my $prev; $_ ne $prev and print $prev = $_ while <>;
      Err.. sorry, got carried away for a second.. Perl is just too seductive. Sigh. :-)

      Makeshifts last the longest.

        I was going to condense it down to a single line:
        % perl -ne '$p ne$_&&print;$p=$_' ...
Re: Re: to avoid redundacy in a file
by Purdy (Hermit) on Jul 15, 2002 at 11:00 UTC
    This won't do exactly as the AM wants - some lines will be duplicate to the user, but not to Perl:
    $ more file.txt N AB TX NC AB N TX NC FOO BAR N AB TX NC $ perl test.pl file.txt N AB TX NC AB N TX NC FOO BAR

    The first two lines of the file.txt file are "the same" to the user, but not to your program. zejames' solution works to the AM's needs, as it creates an unique key for the hash, based on the AM's definition of a duplicate.

    Jason

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://181722]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.