Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

28 but it works.

# 1 2 #1234567890123456789012345678 map/(^\s*[^:]*[^:\s])/,@list

Specifically, remove blank lines, and truncate each line at the first : (if present) and get rid of trailing whitespace after the xxxxx part (including the possible "\n".

Of all the solutions that were submitted, only this one and blokhead's (36) took into account that the 'xxxxxx' portion might itself contain whitespace. (Well, to be fair, busunsl's did too, but it didn't properly remove newlines. Update: So did Arien's as he kindly pointed out to me but his breaks by being too liberal in what it accepts/returns.)

This is what I used to test:

#!/usr/bin/perl -w use strict; my @T = ( '1 :yyyyy blah blah', '2 : yyyyy blah blah', '3 : ', '4 :', '5:yyyyy blah blah', '6: yyyyy blah blah', '7: ', '8:', ' : yyyyy blah blah', ' :yyyyy blah blah', ': yyyyy blah blah', ':yyyyy blah blah', '9 yyyyy blah blah', '10 yyyyy blah blah', '11 ', '12', ' : ', ' :', ': ', ':', ' ', '', '13 andmore:', '14 andmore : blah', ' 15 : foo', ' 16 andmore : foo', ); @T = map {($_,$_."\n")} @T; sub by { print "--- By @_ -------------\n"; } my @list; # 1 2 + 3 # 123456789012345678901234567 +8901234567 @list=@T;by 'sauoq'; print "($_)\n" for map/(^\s*[^:]*[^:\s])/,@lis +t; @list=@T;by 'blokhead'; print "($_)\n" for map{/([^:]*?)\s*(:|$)/;$1|| +()}@list; # Broken @list=@T;by 'busuns1'; print "($_)\n" for map{s/\s*(:.*|$)//;$_||()}@ +list; @list=@T;by 'Arien'; print "($_)\n" for map/(.+?)\s*(?>:|$)/,@list; @list=@T;by 'Arien'; print "($_)\n" for map/(.+?)\b\s*(?>:|$)/,@lis +t; @list=@T;by 'Aristotle';print "($_)\n" for map/^([^:\s]+)/,@list; @list=@T;by 'Aristotle';print "($_)\n" for map/^\s*([^:\s]+)/,@list; @list=@T;by 'blokhead'; print "($_)\n" for map{/([^:]*?)(\s*\n|\s*:)/& +&$1}@list; @list=@T;by 'blokhead'; print "($_)\n" for map{/([^:]*?)\s*(\n|:)/&&$1 +}@list; @list=@T;by 'jmcnmara'; print "($_)\n" for map{(split)[0]}@list; @list=@T;by 'CountZero';print "($_)\n" for map/(.*?)\s+:/,@list;

This is the output:

--- By sauoq ------------- (1) (1) (2) (2) (3) (3) (4) (4) (5) (5) (6) (6) (7) (7) (8) (8) (9 yyyyy blah blah) (9 yyyyy blah blah) (10 yyyyy blah blah) (10 yyyyy blah blah) (11) (11) (12) (12) (13 andmore) (13 andmore) (14 andmore) (14 andmore) ( 15) ( 15) ( 16 andmore) ( 16 andmore) --- By blokhead ------------- (1) (1) (2) (2) (3) (3) (4) (4) (5) (5) (6) (6) (7) (7) (8) (8) (9 yyyyy blah blah) (9 yyyyy blah blah) (10 yyyyy blah blah) (10 yyyyy blah blah) (11) (11) (12) (12) (13 andmore) (13 andmore) (14 andmore) (14 andmore) ( 15) ( 15) ( 16 andmore) ( 16 andmore) --- By busuns1 ------------- (1) (1 ) (2) (2 ) (3) (3 ) (4) (4 ) (5) (5 ) (6) (6 ) (7) (7 ) (8) (8 ) ( ) ( ) ( ) ( ) (9 yyyyy blah blah) (9 yyyyy blah blah) (10 yyyyy blah blah) (10 yyyyy blah blah) (11) (11) (12) (12) ( ) ( ) ( ) ( ) (13 andmore) (13 andmore ) (14 andmore) (14 andmore ) ( 15) ( 15 ) ( 16 andmore) ( 16 andmore ) --- By Arien ------------- (1) (1) (2) (2) (3) (3) (4) (4) (5) (5) (6) (6) (7) (7) (8) (8) ( ) ( ) ( ) ( ) (: yyyyy blah blah) (: yyyyy blah blah) (:yyyyy blah blah) (:yyyyy blah blah) (9 yyyyy blah blah) (9 yyyyy blah blah) (10 yyyyy blah blah) (10 yyyyy blah blah) (11) (11) (12) (12) ( ) ( ) ( ) ( ) (:) (:) (:) (:) ( ) ( ) (13 andmore) (13 andmore) (14 andmore) (14 andmore) ( 15) ( 15) ( 16 andmore) ( 16 andmore) --- By Arien ------------- (1) (1) (2) (2) (3) (3) (4) (4) (5) (5) (6) (6) (7) (7) (8) (8) ( : yyyyy blah blah) ( : yyyyy blah blah) ( :yyyyy blah blah) ( :yyyyy blah blah) (: yyyyy blah blah) (: yyyyy blah blah) (:yyyyy blah blah) (:yyyyy blah blah) (9 yyyyy blah blah) (9 yyyyy blah blah) (10 yyyyy blah blah) (10 yyyyy blah blah) (11) (11) (12) (12) (13 andmore) (13 andmore) (14 andmore) (14 andmore) ( 15) ( 15) ( 16 andmore) ( 16 andmore) --- By Aristotle ------------- (1) (1) (2) (2) (3) (3) (4) (4) (5) (5) (6) (6) (7) (7) (8) (8) (9) (9) (10) (10) (11) (11) (12) (12) (13) (13) (14) (14) --- By Aristotle ------------- (1) (1) (2) (2) (3) (3) (4) (4) (5) (5) (6) (6) (7) (7) (8) (8) (9) (9) (10) (10) (11) (11) (12) (12) (13) (13) (14) (14) (15) (15) (16) (16) --- By blokhead ------------- (1) (1) (2) (2) (3) (3) (4) (4) (5) (5) (6) (6) (7) (7) (8) (8) () () () () () () () () () (9 yyyyy blah blah) () (10 yyyyy blah blah) () (11) () (12) () () () () () () () () () () () () (13 andmore) (13 andmore) (14 andmore) (14 andmore) ( 15) ( 15) ( 16 andmore) ( 16 andmore) --- By blokhead ------------- (1) (1) (2) (2) (3) (3) (4) (4) (5) (5) (6) (6) (7) (7) (8) (8) () () () () () () () () () (9 yyyyy blah blah) () (10 yyyyy blah blah) () (11) () (12) () () () () () () () () () () () () (13 andmore) (13 andmore) (14 andmore) (14 andmore) ( 15) ( 15) ( 16 andmore) ( 16 andmore) --- By jmcnmara ------------- (1) (1) (2) (2) (3) (3) (4) (4) (5:yyyyy) (5:yyyyy) (6:) (6:) (7:) (7:) (8:) (8:) (:) (:) (:yyyyy) (:yyyyy) (:) (:) (:yyyyy) (:yyyyy) (9) (9) (10) (10) (11) (11) (12) (12) (:) (:) (:) (:) (:) (:) (:) (:) (13) (13) (14) (14) (15) (15) (16) (16) --- By CountZero ------------- (1) (1) (2) (2) (3) (3) (4) (4) () () () () () () () () (14 andmore) (14 andmore) ( 15) ( 15) ( 16 andmore) ( 16 andmore)
-sauoq
"My two cents aren't worth a dime.";

In reply to Re: golf anyone? (taking first field) by sauoq
in thread golf anyone? (taking first field) by John M. Dlugosz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found