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

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

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

    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

        I guess it would have helped if I was more specific in exactly what my file looks like. Sorry about that. In reality, there are thousands of groups and my input file looks like this:

        >Group 42 0 25ap, >name_06-T_1_0... at 92.00% 1 28ap, >name_06-T_1_0... * >Group 43 0 28ap, >name_07-N_1_0... * >Group 44 0 29ap, >name_07-N_1_0... * >Group 45 0 25ap, >name_03-T_1_0... * 1 25ap, >name_06-T_1_0... at 100.00% 2 25ap, >name_07-N_1_0... at 100.00% 3 25ap, >name_11-N_1_0... at 100.00% 4 25ap, >name_14-T_1_0... at 100.00%

        I would want an output that looks like this:

        >Group 42 0 25ap, >name_06-T_1_0... at 92.00% 1 28ap, >name_06-T_1_0... * >Group 45 0 25ap, >name_03-T_1_0... * 1 25ap, >name_06-T_1_0... at 100.00% 2 25ap, >name_07-N_1_0... at 100.00% 3 25ap, >name_11-N_1_0... at 100.00% 4 25ap, >name_14-T_1_0... at 100.00%