Two mini-golfish problems

J

J Krugman

OK, here's a little problem that I'd like to propose in the Perl
golf (mini-golf?) vein.

Given a string $x that we may assume contains no newline characters,
split it into chunks of $n characters long separated my newlines.
E.g. if $n == 2,

'fu'
'fubar' ---> 'ba'
'r'

What's the most succinct Perl to achive this end-result?

Thanks,

jill
 
U

Uri Guttman

JK> OK, here's a little problem that I'd like to propose in the Perl
JK> golf (mini-golf?) vein.

JK> Given a string $x that we may assume contains no newline characters,
JK> split it into chunks of $n characters long separated my newlines.
JK> E.g. if $n == 2,

<untested> and prolly long but at least it is clear

my @lines = map "$_\n", $x =~ /(.{1,2})/g ;

or use s/// if you don't mind destroying $x

$x =~ s/(.{1,2})/$1\n/g ;
$x =~ s/(.{1,$n})/$1\n/g ;

that uses greediness to handle any remainder chunk

uri
 
B

Bill

J said:
OK, here's a little problem that I'd like to propose in the Perl
golf (mini-golf?) vein.
Given a string $x that we may assume contains no newline characters,
split it into chunks of $n characters long separated my newlines.
E.g. if $n == 2,

'fu'
'fubar' ---> 'ba'
'r'

What's the most succinct Perl to achive this end-result?

Remember: succint, maintainable, fast. Choose 2 of 3 :)

anyhow...

$x = 'fubar';
$x=~s/(..)/$1\n/g;print $x;
 
U

Uri Guttman

B> Remember: succint, maintainable, fast. Choose 2 of 3 :)

how about correct?

B> $x = 'fubar';
B> $x=~s/(..)/$1\n/g;print $x;

what happens to the 'r'?

uri
 
T

Tassilo v. Parseval

Also sprach Bill:
Remember: succint, maintainable, fast. Choose 2 of 3 :)

anyhow...

$x = 'fubar';
$x=~s/(..)/$1\n/g;print $x;

That should be:

$x=~s/(..?)/$1\n/g;

Otherwise it omits the last newline for words of uneven length.

Tassilo
 
J

Jeff 'japhy' Pinyan

Also sprach Bill:


That should be:

$x=~s/(..?)/$1\n/g;

Otherwise it omits the last newline for words of uneven length.

So? Krugman said "separated". She didn't say that each chunk has a
newline, just that they're separated.
 
T

Tassilo v. Parseval

Also sprach Jeff 'japhy' Pinyan:
So? Krugman said "separated". She didn't say that each chunk has a
newline, just that they're separated.

Well, what is a chunk separated by a newline anyway? Did she mean:

"fu\n",
"ba\n",
"r\n"

or did she mean

"fu\nba\nr\n"

or rather

"fu",
"ba",
"r"

Note that there are no newlines in the last alternative, but she
explicitely mentions them.

Apparently you know what she meant. Care to enlighten me? :)

Tassilo
 
A

Anno Siegel

Uri Guttman said:
JK> OK, here's a little problem that I'd like to propose in the Perl
JK> golf (mini-golf?) vein.

JK> Given a string $x that we may assume contains no newline characters,
JK> split it into chunks of $n characters long separated my newlines.
JK> E.g. if $n == 2,

<untested> and prolly long but at least it is clear

my @lines = map "$_\n", $x =~ /(.{1,2})/g ;

Same length, no regex:

my @lines = map "$_\n", unpack '(A2)*', $x;

If it's less clear, then only because nobody knows the pack formats
by heart :)

Anno
 
J

J Krugman

I am sorry about the confusing statement. I meant "fu\nba\nr".

I originally tried s/(?<=[^\n]{2})/\n/g, but this failed miserably;
my mistake was to conclude from this that regexps were not the way
to go. I'm still amazed that the regexp above gives different
results from s/(.{2})/$1\n/g. I often use zero-width look-ahead
and look-behinds to avoid carrying $1's over to the second slot of
a s/// expression. It's perverse, I know, but until this case I
always got the desired results, but I guess I'd never tried this
trick with a s///g substitution.

Thanks,

jill
 
A

Anno Siegel

Anno Siegel said:
Same length, no regex:

my @lines = map "$_\n", unpack '(A2)*', $x;

If it's less clear, then only because nobody knows the pack formats
by heart :)

Let me retract that, it's cheating. The last chunk is filled with
invisible null bytes. That bites.

Anno
 
B

Bill

Uri said:
B> Remember: succint, maintainable, fast. Choose 2 of 3 :)

how about correct?

B> $x = 'fubar';
B> $x=~s/(..)/$1\n/g;print $x;

what happens to the 'r'?

uri

On my system, the OS inserts a \n at script exit, so that one does not
need to be added by perl in the print. Yes, I know it's not perfect
otherwise :).
 
U

Uri Guttman

J'P> So? Krugman said "separated". She didn't say that each chunk has a
J'P> newline, just that they're separated.


and she drew this little picture which of course looks like the last
part has a newline. poor specs are too blame and she has copped to that
in another post.

'fu'
'fubar' ---> 'ba'
'r'

uri
 
B

Brad Baxter

Remember: succint, maintainable, fast. Choose 2 of 3 :)

anyhow...

$x = 'fubar';
$x=~s/(..)/$1\n/g;print $x;

But OP did mention $n ...

sub _ {(my$x=$_[0])=~s/(.{$_[1]})/$1\n/g;$x}
print _ fubar=>2


Brad
 
A

Ala Qumsieh

J Krugman said:
OK, here's a little problem that I'd like to propose in the Perl
golf (mini-golf?) vein.

Btw, your title says "Two" problems .. what is the second one?

--Ala
 
X

Xavier Noria

Ala Qumsieh said:
$x=~s/..?/$&
/g;

:)

Heh, but $n seems to be a parameter. If it was what about

$x=~s/(??{".?"x$n})/$&
/g;

What does the spec say about the empty string? :)

-- fxn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,382
Latest member
MichaleStr

Latest Threads

Top