Converting a textfile-like string to an array and back

2003-02-07 Thread Andrew Savige
I am interested to learn the fastest and shortest way to convert a textfile-like string to an array and back again (chopping newlines). Test program follows. Improvements (golf or speed) welcome. /-\ use strict; my $x = 'FLAMING_OSTRICHES'; This is first test line This is 2nd And 3rd

Re: Converting a textfile-like string to an array and back

2003-02-07 Thread A. Pagaltzis
* Andrew Savige [EMAIL PROTECTED] [2003-02-07 13:47]: my @lines = split(/^/, $x, -1); chomp(@lines); # fastest? my @lines = split m[\Q$/], $x, -1; -- Regards, Aristotle

Re: Converting a textfile-like string to an array and back

2003-02-07 Thread Yanick
On Fri, Feb 07, 2003 at 06:26:22PM +1100, Andrew Savige wrote: my @lines = split(/^/, $x, -1); chomp(@lines); # fastest? I'll prolly say something stupid here, but: my @lines = split \n, $x; Joy, `/. -- There are people in the world so hungry, that God cannot appear to

Re: Converting a textfile-like string to an array and back

2003-02-07 Thread John Douglas Porter
Andrew Savige [EMAIL PROTECTED] wrote: I am interested to learn the fastest and shortest way to convert a textfile-like string to an array and back again (chopping newlines). Not tested, but I would guess that the obvious @lines = split /\n/, $x, -1; pop @lines; might be both fastest and

RE: Converting a textfile-like string to an array and back

2003-02-07 Thread Winter Christian
-Original Message- From: Andrew Savige [mailto:[EMAIL PROTECTED]] I am interested to learn the fastest and shortest way to convert a textfile-like string to an array and back again (chopping newlines). Test program follows. Improvements (golf or speed) welcome. # String to array:

Re: Converting a textfile-like string to an array and back

2003-02-07 Thread A. Pagaltzis
* John Douglas Porter [EMAIL PROTECTED] [2003-02-07 14:15]: @lines = split /\n/, $x, -1; pop @lines; $/ can be different from \n though. And popping the last field is dangerous - you don't know if the file ends with a newline. Also, you now have no chance to reconstruct the exact equivalent

Re: Converting a textfile-like string to an array and back

2003-02-07 Thread John Douglas Porter
A. Pagaltzis [EMAIL PROTECTED] wrote: * John Douglas Porter [EMAIL PROTECTED] [2003-02-07 14:15]: @lines = split /\n/, $x, -1; pop @lines; $/ can be different from \n though. Yes, but his example data was text in a here document. But you can always do split m,$/, $x, -1; And

Re: Converting a textfile-like string to an array and back

2003-02-07 Thread A. Pagaltzis
* John Douglas Porter [EMAIL PROTECTED] [2003-02-07 14:50]: Yes, but his example data was text in a here document. Then add a note about the caveat. split m,$/, $x, -1; In bizarre cases, $/ might contain regex metacharacters. Don't forget the \Q. And popping the last field is dangerous -

Re: Converting a textfile-like string to an array and back

2003-02-07 Thread John Douglas Porter
A. Pagaltzis [EMAIL PROTECTED] wrote: John Douglas Porter [EMAIL PROTECTED]: Yes, but his example data was text in a here document. Then add a note about the caveat. Sorry, I thought (and still do) that the OP's caveat was understood to still be in effect. join \n, @lines, $tail;

Re: Converting a textfile-like string to an array and back

2003-02-07 Thread A. Pagaltzis
* Yitzchak Scott-Thoennes [EMAIL PROTECTED] [2003-02-07 20:51]: chomp(my $tmp=$x); my @lines=split /\n/,$tmp,-1 Very nice. -- Regards, Aristotle