Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^3: Shorten a list...

by Cristoforo (Deacon)
on Oct 17, 2011 at 21:30 UTC ( #932015=note: print w/ replies, xml ) Need Help??


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


Comment on Re^3: Shorten a list...
Select or Download Code
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%

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://932015]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (20)
As of 2015-07-06 18:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (80 votes), past polls