in reply to golf anyone? (taking first field)
I tested it with the following data and it seemed to work correctly:map{/([^:]*?)(\s*\n|\s*:)/&&$1}@list; # silly me ... distributive property! update: 34 chars: map{/([^:]*?)\s*(\n|:)/&&$1}@list;
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.__DATA__ 123 : oain:b:okfbd 456 foo bar : df a dsaf asdf 111 :
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 | |
by blokhead (Monsignor) on Jan 07, 2003 at 07:32 UTC | |
by John M. Dlugosz (Monsignor) on Jan 07, 2003 at 08:25 UTC |