Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: golf anyone? (taking first field)

by blokhead (Monsignor)
on Jan 07, 2003 at 06:51 UTC ( [id://224877]=note: print w/replies, xml ) Need Help??


in reply to golf anyone? (taking first field)

Here's my shot (37 chars):
map{/([^:]*?)(\s*\n|\s*:)/&&$1}@list; # silly me ... distributive property! update: 34 chars: map{/([^:]*?)\s*(\n|:)/&&$1}@list;
I tested it with the following data and it seemed to work correctly:
__DATA__ 123 : oain:b:okfbd 456 foo bar : df a dsaf asdf 111 :
It doesn't work when the input line contains only whitespace, but it does work for blank (other than the \n) lines. It returns empty string when the colon is the first non-whitespace. I don't know if this is correct behavior or not, though! This is my first golf attempt, so I may have missed some regex shortening tricks.

Update: Here's a slightly different approach that's the same 34 chars, although it behaves differently. This one does ignore lines of complete whitespace. It still gives empty string when a colon is the first non-whitespace in the line.

map{(split/\s*(:|\n|$)/)[0]}@list;

blokhead

Replies are listed 'Best First'.
Re: Re: golf anyone? (taking first field)
by John M. Dlugosz (Monsignor) on Jan 07, 2003 at 07:20 UTC
    If the colon begins the line, it should be the same as an empty line. That is, remove the '' from the output list.
      In that case,
      map{/([^:]*?)\s*(:|$)/;$1||()}@list;
      Back to 36 chars. ;)

      blokhead

        Yea, that's what I noticed when I decided to throw it out as a golf: stopping at the colon or the end-of-line complicates things in a slightly subtle manner. Doing it in multiple passes it's no big deal: chomp first, then delete the tail end stuff, then throw it away if empty. But that's not elegant, and it doesn't just "fit together" as a composition of functions (one feeding the result to the next outer one).

        I think this also has an interestingly high punctuation to alpha ratio.

        I see you make sure the pattern always matches, so $1 is always OK (just empty).

        —John

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-24 17:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found