Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^4: Shorten a list...

by la (Novice)
on Oct 17, 2011 at 21:51 UTC ( [id://932023]=note: print w/replies, xml ) Need Help??


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

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)?

Replies are listed 'Best First'.
Re^5: Shorten a list...
by Cristoforo (Curate) on Oct 18, 2011 at 02:58 UTC
    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%
        Yes, it is necessary to see exactly what your file looks like. Could have saved some typing knowing that :-)

        A possible solution is close to my first answer.

        #!/usr/bin/perl use strict; use warnings; { local ($/, $\) = ('', "\n\n"); while (<DATA>) { chomp; print if tr/\n// > 1; } } __DATA__ >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%
        The statement local ($/, $\) = ('', "\n\n");, sets the INPUT_RECORD_SEPARATOR, $/, to read in paragraphs. A paragraph is lines of text followed by 1 or more blank lines, (2 or more newlines). The OUTPUT_RECORD_SEPARATOR, $\, is set to "\n\n". $\ is printed at the end of every print statement.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-04-24 07:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found