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 FLAMING

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

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 a

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 arr

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;

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 danger

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

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

2003-02-07 Thread Yitzchak Scott-Thoennes
On Fri, 7 Feb 2003 15:05:47 +0100, [EMAIL PROTECTED] wrote: >Not if the last line did not end with a record separator. >His example data did, but other data might not. In general, I'm all in favor of coding charitably toward files w/o a trailing newline. Here, however, the OP wants to convert dat

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

2003-02-07 Thread John Douglas Porter
Yitzchak Scott-Thoennes <[EMAIL PROTECTED]> wrote: > chomp( my $tmp = $x ); > my @lines = split /\n/, $tmp, -1; Ah, yes, of course! Yitzchak++ -- John Douglas Porter __ Do you Yahoo!? Yahoo! Mail Plus - Powerful. Affordable. Sign up now. http

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

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

2003-02-08 Thread Ilmari Karonen
On Fri, 7 Feb 2003, A. Pagaltzis wrote: > * John Douglas Porter <[EMAIL PROTECTED]> [2003-02-07 14:50]: > > > join "\n", @lines, $tail; > > Unfortunately, that won't work either - regardless of the > value of $tail - even undef -, you are joining an extra > element onto the string. Yes it doe

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

2003-02-08 Thread Andrew Savige
Aristotle wrote: > my @lines = split m[\Q$/], $x, -1; This one produces one too many elements in the array. /-\ http://greetings.yahoo.com.au - Yahoo! Greetings - Send some online love this Valentine's Day.

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

2003-02-08 Thread Andrew Savige
`/anick wrote: > I'll prolly say something stupid here, but: > > my @lines = split "\n", $x; This one prolly erroneously removes empty trailing lines. /-\ http://greetings.yahoo.com.au - Yahoo! Greetings - Send some online love this Valentine's Day.

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

2003-02-08 Thread Andrew Savige
Winter Christian wrote: > # String to array: > @lines=$x=~/[^\n]/g; This one splits into array of chars not array of lines. /-\ http://greetings.yahoo.com.au - Yahoo! Greetings - Send some online love this Valentine's Day.

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

2003-02-08 Thread Andrew Savige
Yitzchak Scott-Thoennes wrote: > chomp(my $tmp=$x); my @lines=split /\n/,$tmp,-1 This one fails for the case $x = "\n"; @lines should contain one empty element, but the code above contains none. /-\ http://greetings.yahoo.com.au - Yahoo! Greetings - Send some online love this Valentine's D

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

2003-02-08 Thread Andrew Savige
To clarify, you may assume that lines in string are separated by "\n" but any solution must pass the following edge cases: 1) empty string: @lines should contain zero elements 2) string of "\n" : @lines should contain one empty element 3) trailing empty lines should be retained 4) you may not a

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

2003-02-08 Thread Andrew Savige
To convert the other way is simpler and I'm struggling to find alternatives to my original join solution. The only edge case I can think of is empty array @lines. You may assume that no item of @lines contains "\n". Benchmark program follows. use strict; use Benchmark; my @lines = ( "", "This

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

2003-02-08 Thread Yitzchak Scott-Thoennes
On Sun, 9 Feb 2003 12:34:38 +1100 (EST), [EMAIL PROTECTED] wrote: >Yitzchak Scott-Thoennes wrote: >> chomp(my $tmp=$x); my @lines=split /\n/,$tmp,-1 > >This one fails for the case $x = "\n"; @lines should contain one >empty element, but the code above contains none. Sigh. I'd call that a bug

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

2003-02-08 Thread Yitzchak Scott-Thoennes
On Sun, 9 Feb 2003 14:34:07 +1100 (EST), [EMAIL PROTECTED] wrote: >sub a2 { my $y = ""; $y .= $_ . "\n" for @lines } The ="" is optional. my $x; $x.="foo" intentionally doesn't warn.

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

2003-02-08 Thread John W. Krahn
Andrew savige wrote: > > sub a1 { my @lines = split(/^/, $x, -1); chomp(@lines) } > sub a2 { my @lines = $x eq "" ? () : $x =~ /^.*/mg } > sub j1 { my @lines = map { chomp; $_ } split /^/, $x, -1 } > # w1 is Perl 5.8.0 only > sub w1 { open(my $fh, "<", \$x); my @lines = <$fh>; chomp(@lines) } > ti

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

2003-02-09 Thread A. Pagaltzis
* John W. Krahn <[EMAIL PROTECTED]> [2003-02-09 12:27]: > You can also speed up j1 by using map without the braces. > > sub j1 { my @lines = map chomp && $_, split /^/, $x, -1 } Which will promptly fail on a last line without a trailing record separator because chomp would return 0 and cause the

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

2003-02-09 Thread Andrew Savige
Yitzchak Scott-Thoennes wrote: > Sigh. I'd call that a bug if someone hadn't gone to the trouble to > test for it and document it. (Indeed, I see a bug report out there: > #6653, was 20010327.008.) So do something like: > > my @lines; > chomp(my $tmp=$x); > @lines = split /\n/,$tmp,-1 or @lines=

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

2003-02-09 Thread John Douglas Porter
@lines = length($x) ? $x=~/^(.*)$/mg : (); -- John Douglas Porter __ Do you Yahoo!? Yahoo! Mail Plus - Powerful. Affordable. Sign up now. http://mailplus.yahoo.com

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

2003-02-09 Thread Andrew Savige
John Douglas Porter wrote: > @lines = length($x) ? $x=~/^(.*)$/mg : (); ^ ^^ unnecessary This is a little faster: @lines = length($x) ? $x=~/^.*/mg : (); /-\ http://greetings.yahoo.com.au - Yahoo! Greetings - Send some online love th

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

2003-02-09 Thread Andrew Savige
John Douglas Porter: > @lines = length($x) ? $x=~/^(.*)$/mg : (); @lines = ($x=~/^.*/mg) x !!length($x); @lines = ($x=~/^.*/mg) x ($x ne ""); This line of play looks promising for golf. /-\ http://greetings.yahoo.com.au - Yahoo! Greetings - Send some online love this Valentine's Day.

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

2003-02-09 Thread A. Pagaltzis
* Andrew Savige <[EMAIL PROTECTED]> [2003-02-10 07:28]: > @lines = ($x=~/^.*/mg) x !!length($x); $_=$x;@lines=(/^.*/mg)x+length; -- Regards, Aristotle

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

2003-02-09 Thread Andrew Savige
Aristotle: > > @lines = ($x=~/^.*/mg) x !!length($x); > > $_=$x;@lines=(/^.*/mg)x+length; *whistle* *whistle* *red card* *disqualified* multiplying by length (x+length) will not give the desired result here; it must be boolean (0 or 1 only). /-\ http://greetings.yahoo.com.au - Yahoo! Gre

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

2003-02-09 Thread Andrew Savige
Aristotle golfed: > $_=$x;@lines=(/^.*/mg)x+length; Against my better judgment, I will have a go at golfing this: $_=$x;@l=(/^.*/mg)x/./s /-\ http://greetings.yahoo.com.au - Yahoo! Greetings - Send some online love this Valentine's Day.

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

2003-02-10 Thread A. Pagaltzis
* Andrew Savige <[EMAIL PROTECTED]> [2003-02-10 17:54]: > *whistle* *whistle* *red card* *disqualified* > multiplying by length (x+length) will not give the desired > result here; it must be boolean (0 or 1 only). Doh! Of course. I'm an idiot. *grmbl* -- Regards, Aristotle

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

2003-02-10 Thread Eugene van der Pijll
Andrew Savige schreef: > Aristotle golfed: > > $_=$x;@lines=(/^.*/mg)x+length; > > Against my better judgment, I will have a go at golfing this: > > $_=$x;@l=(/^.*/mg)x/./s This clobbers $_. Not nice for the rest of the program. Correct is: {local$_=$x;@l=(/^.*/mg)x/./s} or @l=(/^.*/

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

2003-02-10 Thread A. Pagaltzis
* Eugene van der Pijll <[EMAIL PROTECTED]> [2003-02-10 18:35]: > Unfortunately, you had "use strict" in your first post, > and neither of these are use-strict safe. Oh? What makes you say so? -- Regards, Aristotle

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

2003-02-10 Thread Andrew Savige
(-ugene sprak: > Andrew Savige schreef: > > Aristotle golfed: > > > $_=$x;@lines=(/^.*/mg)x+length; > > > > Against my better judgment, I will have a go at golfing this: > > > > $_=$x;@l=(/^.*/mg)x/./s > > This clobbers $_. Not nice for the rest of the program. Correct is: > > {local$_=$

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

2003-02-10 Thread Yitzchak Scott-Thoennes
On Mon, 10 Feb 2003 11:09:25 +1100 (EST), [EMAIL PROTECTED] wrote: >Yitzchak Scott-Thoennes wrote: >> Sigh. I'd call that a bug if someone hadn't gone to the trouble to >> test for it and document it. (Indeed, I see a bug report out there: >> #6653, was 20010327.008.) So do something like: >> >>

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

2003-02-10 Thread Keith C. Ivey
Yitzchak Scott-Thoennes <[EMAIL PROTECTED]> wrote: > Only if you say $x eq "" means no lines instead of one empty > line missing its "\n" :) Well, the subject line does say "textfile-like", and a 0-byte text file has no lines, not one empty line. -- Keith C. Ivey <[EMAIL PROTECTED]> Washington

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

2003-02-10 Thread Yitzchak Scott-Thoennes
On Mon, 10 Feb 2003 23:44:14 -0500, [EMAIL PROTECTED] wrote: >Yitzchak Scott-Thoennes <[EMAIL PROTECTED]> wrote: > >> Only if you say $x eq "" means no lines instead of one empty >> line missing its "\n" :) > >Well, the subject line does say "textfile-like", and a 0-byte >text file has no lines, n

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

2003-02-11 Thread Ian Phillipps
On Tue, 11 Feb 2003 at 10:39:53 +1100, Andrew Savige wrote: > In case anyone is unaware of (-ugene's status in world golf, you can > find out by typing in Eugene van der Pijll at www.googlism.com. I just typed in 'Eugene' to check. eugene is the fastest and most pleasant eugene is about the size

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

2003-02-11 Thread Andrew Savige
Ian Phillipps sprak: > I just typed in 'Eugene' to check. > > eugene is the fastest and most pleasant > eugene is about the size of a dog > eugene is devastated > > But, most relevantly, > > eugene is right In my experience, Eugene (and Ton) are always right. I just noticed that Yitzchak's emai

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

2003-02-11 Thread Winter Christian
Andrew Savige [mailto:[EMAIL PROTECTED]] wrote: > Winter Christian wrote: > > # String to array: > > @lines=$x=~/[^\n]/g; > > This one splits into array of chars not array of lines. Shame on me, seems like I was unable to read what the question was. -Christian