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

FLAMING_OSTRICHES

# convert a string to an array (removing new lines)
my @lines = split(/^/, $x, -1); chomp(@lines);      # fastest?
# my @lines = $x =~ /^.*/mg;                        # shortest?
# my @lines = map { chomp; $_ } split /^/, $x, -1;

# interestingest? -- perl 5.8.0 only
# open(my $fh, "<", \$x); my @lines = <$fh>; chomp(@lines);

print "num lines=", scalar(@lines), "\n";
for my $k (@lines) { print "k='$k'\n" }

# convert it back again
my $y = join("\n", @lines, "");    # improvements welcome
$x eq $y or die "oops";



http://greetings.yahoo.com.au - Yahoo! Greetings
- Send some online love this Valentine's Day.

Reply via email to