in reply to Shorten a list...

Here's a version that preserves the line order within the file while allowing out of order lines (for example, "Group 1" appears on the first and last line below) and making empty lines separating groups optional (for example, there are blank lines within the "Group 5" lines below):

use strict; use warnings; my %seen; print map { $seen{$_} > 1 ? $_ x $seen{$_} . "\n" : () } grep { not $seen{$_}++ } grep { !/^\s*$/ } <DATA>; __DATA__ Group 1 Group 2 Group 3 Group 3 Group 3 Group 4 Group 5 Group 5 Group 5 Group 5 Group 1
Running the above program produces:
Group 1 Group 1 Group 3 Group 3 Group 3 Group 5 Group 5 Group 5 Group 5

Replies are listed 'Best First'.
Re^2: Shorten a list...
by la (Novice) on Oct 17, 2011 at 03:06 UTC

    Thanks so much for all of the feedback. As I have been working on this, the format has been changed so that the original input list now looks like:

    Group 1
    name
    Group 2
    name
    Group 3
    name
    name
    Group 4
    name
    Group 5
    name
    name
    name

    Your suggestions so far have been really helpful. Can anyone help me now with trying to only print the groups with multiple entries (Group 3 and Group 5) in this format:

    Group 3
    name
    name

    Group 5
    name
    name
    name

    Again, your help is greatly appreciated

      There may be other ways than this. If the file is large (MB or GB), you might not want this method. It reads the whole file into an array, @data, before printing.
      #!/usr/bin/perl use strict; use warnings; my (@buffer, @data); while (<DATA>) { if (/^Group/) { push @data, [@buffer] if @buffer > 2; @buffer = $_; } else { push @buffer, $_; } } push @data, [@buffer] if @buffer > 2; { local $" = ''; print join("\n", map "@$_", @data); }
      Chris

        Thanks for the response. I have tried this script and still have the singlets being printed. Is there a way to get rid of the singlets (eg. only print groups 3 and 5 final output)?