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
* 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
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
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
-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:
* 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
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
* 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 -
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;
* Yitzchak Scott-Thoennes [EMAIL PROTECTED] [2003-02-07 20:51]:
chomp(my $tmp=$x); my @lines=split /\n/,$tmp,-1
Very nice.
--
Regards,
Aristotle
10 matches
Mail list logo