Re: Interactive golf hole
In article [EMAIL PROTECTED], [EMAIL PROTECTED] (Yitzchak Scott-Thoennes) wrote: Ronald J Kimball [EMAIL PROTECTED] wrote: Here's my current solution, at 64: -pl $_=join':',grep defined,/([^]*)(?![^\s#])|([^\s#]+)|#.*/g Originally I had $_.0 instead of defined, but I realized that that skips empty fields, i.e. . Is there a shorter way to test for defined? Not that I can think of. BTW, (?=\s|#|$) is shorter than (?=[\s#]|$). So is (?![^\s#]). That gets me to 62: -pl s/\s*(([^]*)(?![^\s#])|([^\s#]+))\s*(#.*)?/$2$3:/g,chop Which fails for lines with more than one character but no fields. With no easy fix. I think you've got the winner, Ronald.
Re: Interactive golf hole
On Jan 8, Yitzchak Scott-Thoennes said: I prefer (at least sometimes) to see golf worked out interactively, on the list. I'm not sure I understand. Shall I show my code? A file has 0 or more fields on each line. Fields are separated by 1 or more whitespace characters. Leading and trailing whitespace should be ignored. Comments (starting with # and continuing to the end of the line) should be ignored. A field may have surrounding double-quotes. Such a field may contain # or whitespace (but not ). The surrounding quotes are themselves not part of the field. Any other use of a has no special meaning (e.g.: howdy, partner# foo is a line with two fields, 'howdy,' and 'partner'). Read the file and print the fields of each line separated by ':'. My code is below. More below. # 57 $\=$/;$,=':';print/([^]*|[^\s#]+)\s*(?:#.*)?/g while # 52 -n $\=$/;$,=':';print/([^]*|[^\s#]+)\s*(?:#.*)?/g -- Jeff japhy Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ ** Look for Regular Expressions in Perl published by Manning, in 2002 ** stu what does y/// stand for? tenderpuss why, yansliterate of course.
Re: Interactive golf hole
On Jan 8, Jeff 'japhy' Pinyan said: On Jan 8, Yitzchak Scott-Thoennes said: I prefer (at least sometimes) to see golf worked out interactively, on the list. I'm not sure I understand. Shall I show my code? My \s* should've been a \s+. -- Jeff japhy Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ ** Look for Regular Expressions in Perl published by Manning, in 2002 ** stu what does y/// stand for? tenderpuss why, yansliterate of course.
Re: Interactive golf hole
In article [EMAIL PROTECTED], Jeff 'japhy' Pinyan [EMAIL PROTECTED] wrote: On Jan 8, Yitzchak Scott-Thoennes said: I prefer (at least sometimes) to see golf worked out interactively, on the list. I'm not sure I understand. Shall I show my code? Yes. -n $\=$/;$,=':';print/([^]*|[^\s#]+)\s*(?:#.*)?/g Close. I said: The surrounding quotes are themselves not part of the field, so the quotes should not get printed. Also, I said: Fields are separated by 1 or more whitespace characters, so xx is one 4 character field, not two 1 character fields.
Re: Interactive golf hole
On Tue, Jan 08, 2002 at 02:50:16PM -0500, Jeff 'japhy' Pinyan wrote: On Jan 8, Yitzchak Scott-Thoennes said: A file has 0 or more fields on each line. Fields are separated by 1 or more whitespace characters. Leading and trailing whitespace should be ignored. Comments (starting with # and continuing to the end of the line) should be ignored. A field may have surrounding double-quotes. Such a field may contain # or whitespace (but not ). The surrounding quotes are themselves not part of the field. Any other use of a has no special meaning (e.g.: howdy, partner# foo is a line with two fields, 'howdy,' and 'partner'). Read the file and print the fields of each line separated by ':'. My code is below. Those solutions don't quite work, because they include the surrounding double quotes as part of the field in the output. According to the description, the surrounding quotes are not part of the actual field. Ronald
Re: Interactive golf hole
On Tue, Jan 08, 2002 at 11:50:44AM -0800, Yitzchak Scott-Thoennes wrote: Also, I said: Fields are separated by 1 or more whitespace characters, so xx is one 4 character field, not two 1 character fields. Woops, I got that part wrong. Gotta go rework my solution... Ronald
Re: Interactive golf hole
On Tue, Jan 08, 2002 at 11:50:44AM -0800, Yitzchak Scott-Thoennes wrote: -n $\=$/;$,=':';print/([^]*|[^\s#]+)\s*(?:#.*)?/g Close. I said: The surrounding quotes are themselves not part of the field, so the quotes should not get printed. Also, I said: Fields are separated by 1 or more whitespace characters, so xx is one 4 character field, not two 1 character fields. I think -n $,=':';$\=$/;print grep$_,/\G\s*(?:([^]*)(?:\s|$)|([^\s#]+))/g; manage to do that. (but what a poor score it gives... :/ ) `/anick -- I must not fear. Fear is the mind-killer that brings total obliteration. I will face my fear. I will permit it to pass over me and through me. And when it is gone past I will turn the inner eye to see its path. Where the fear has gone there will be nothing. Only I will remain. - Frank Herbert
Re: Interactive golf hole
On Tue, Jan 08, 2002 at 01:30:12PM -0800, Yitzchak Scott-Thoennes wrote: On Tue, Jan 08, 2002 at 11:50:44AM -0800, Yitzchak Scott-Thoennes wrote: -n $,=':';$\=$/;print grep$_,/\G\s*(?:([^]*)(?:\s|$)|([^\s#]+))/g; (but what a poor score it gives... :/ ) It gets worse: While whitespace separates fields, the last field could have a comment next. Ugh, missed that one too. :) And is an empty field. But I think the \G is unnecessary. I think the \G is necessary because the regex doesn't explicitly match the comment. The \G forces it to stop when it gets to the comment, rather than skipping over the # and matching more fields. Ronald
Re: Interactive golf hole
On Tue, Jan 08, 2002 at 10:37:45AM -0800, Yitzchak Scott-Thoennes wrote: Here's a hole: A file has 0 or more fields on each line. Fields are separated by 1 or more whitespace characters. Leading and trailing whitespace should be ignored. Comments (starting with # and continuing to the end of the line) should be ignored. A field may have surrounding double-quotes. Such a field may contain # or whitespace (but not ). The surrounding quotes are themselves not part of the field. Any other use of a has no special meaning (e.g.: howdy, partner# foo is a line with two fields, 'howdy,' and 'partner'). Read the file and print the fields of each line separated by ':'. Does this work? -p s/\s*(([^]*)|((\S*?)#.*)|(\S+))\s*/$2$4$5:/g,s/:+$/\n/ Wesley.
Re: Interactive golf hole
Wesley Darlington [EMAIL PROTECTED] wrote: -p s/\s*(([^]*)|((\S*?)#.*)|(\S+))\s*/$2$4$5:/g,s/:+$/\n/ That's downright clever. I think the approach could be a winner, but as is it removes trailing empty fields: x becomes: x instead of: x:
Re: Interactive golf hole
Wesley Darlington [EMAIL PROTECTED] wrote: -p s/\s*(([^]*)|((\S*?)#.*)|(\S+))\s*/$2$4$5:/g,s/:+$/\n/ That's downright clever. I think the approach could be a winner, but as is it removes trailing empty fields: x becomes: x instead of: x: Now that someone has braved removing the quotes in the regex (which I was hoping would happen) I'll post my solution that does it the long way: 82: -nl $,=:;print map/^(.*)$/?$1:$_,/\s*([^]*(?=[\s#]|$)|[^\s#]+)\s*(?:#.*)?/g This should be easily beatable.
Re: Interactive golf hole
Wesley Darlington [EMAIL PROTECTED] wrote: -p s/\s*(([^]*)|((\S*?)#.*)|(\S+))\s*/$2$4$5:/g,s/:+$/\n/ Is the third set of parens necessary? Won't this do the same? -p s/\s*(([^]*)|(\S*?)#.*|(\S+))\s*/$2$3$4:/g,s/:+$/\n/ And from there you can cut it further to -p s/\s*(([^]*)|(\S*?)#.*|(\S+))\s*/$+:/g,s/:+$/\n/ It still has the problems with trailing null fields and xx. -- Keith C. Ivey [EMAIL PROTECTED] Washington, DC
Re: Interactive golf hole
Ronald J Kimball [EMAIL PROTECTED] wrote: Here's my current solution, at 64: -pl $_=join':',grep defined,/([^]*)(?![^\s#])|([^\s#]+)|#.*/g Originally I had $_.0 instead of defined, but I realized that that skips empty fields, i.e. . Is there a shorter way to test for defined? Not that I can think of. BTW, (?=\s|#|$) is shorter than (?=[\s#]|$). So is (?![^\s#]). That gets me to 62: -pl s/\s*(([^]*)(?![^\s#])|([^\s#]+))\s*(#.*)?/$2$3:/g,chop