in reply to Re: Shorten a list...
in thread Shorten a list...

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

Replies are listed 'Best First'.
Re^3: Shorten a list...
by Cristoforo (Curate) on Oct 17, 2011 at 21:30 UTC
    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)?

        My code ran fine on the sample you provided. Is this exactly how the file looks?
        __DATA__ Group 1 name Group 2 name Group 3 name name Group 4 name Group 5 name name name Group 11 name name Group 15 name name name