Re: [PATCH] Add .trim method
Larry Wall wrote: On Mon, Jan 12, 2009 at 05:04:50AM -0800, Ovid wrote: : ...the trivial $string.trim and trim($string) case. Hmm, I'd think .trim should work like .chomp, and return the trimmed string without changing the original. You'd use $str.=trim to do it in place. Can't say I really like the negated options though. They smell funny. Larry I'm +1 on adding a trim, I do a lot of csv import (with trimming) in perl 5. On a side note, between modifying the original and returning a fixed string, which one would we expect to be faster? And I assume either would be faster than the regex method of trimming? -Andy
multi return values
(Sorry if this dbl-posts, sent it from the wrong account the first time) Hi all, what's wrong with this code: use v6; sub multireturn($x, $y) { my $a = $x * 2; my $b = $y * 2; return($a, $b); } my($a, $b) = multireturn(2, 3); using: This is Rakudo Perl 6, revision 32970 built on parrot 0.8.1-devel for i486-linux-thread-multi. I get: Method 'lvalue' not found for invocant of class 'PAST;Stmts' current instr.: 'parrot;PAST;Compiler;as_post' pc 2924 (src/PAST/Compiler.pir:742) called from Sub 'parrot;PAST;Compiler;post_children' pc 1783 (src/PAST/Compiler.pir:368) called from Sub 'parrot;PAST;Compiler;as_post' pc 2060 (src/PAST/Compiler.pir:500) called from Sub 'parrot;PAST;Compiler;post_children' pc 1783 (src/PAST/Compiler.pir:368) called from Sub 'parrot;PAST;Compiler;pirop' pc 3061 (src/PAST/Compiler.pir:796) called from Sub 'parrot;PAST;Compiler;post_children' pc 1783 (src/PAST/Compiler.pir:368) called from Sub 'parrot;PAST;Compiler;as_post' pc 2408 (src/PAST/Compiler.pir:614) called from Sub 'parrot;PCT;HLLCompiler;compile' pc 434 (src/PCT/HLLCompiler.pir:303) called from Sub 'parrot;PCT;HLLCompiler;eval' pc 868 (src/PCT/HLLCompiler.pir:502) called from Sub 'parrot;PCT;HLLCompiler;evalfiles' pc 1233 (src/PCT/HLLCompiler.pir:676) called from Sub 'parrot;PCT;HLLCompiler;command_line' pc 1412 (src/PCT/HLLCompiler.pir:765) called from Sub 'parrot;Perl6;Compiler;main' pc 16160 (perl6.pir:168)
Re: multi return values
Moritz Lenz wrote: Andy Colson wrote: (Sorry if this dbl-posts, sent it from the wrong account the first time) Hi all, what's wrong with this code: use v6; sub multireturn($x, $y) { my $a = $x * 2; my $b = $y * 2; return($a, $b); } my($a, $b) = multireturn(2, 3); There's (nearly) nothing wrong with your code, only with the compiler ;-) Rakudo doesn't support list assignment yet (that's where the error message comes from), and doesn't support returning values either. A workaround for now is to use arrays instead. You mean like: my @list = multireturn(2, 3); That still doesn't work. But its not a big deal... I was just playing around trying to learn the language. (The thing that's still wrong with your code is that you need a whitespace after the 'my', otherwise my(...) should be parsed as a function call). OH! Good call, I'd forgotten about that. That's going to take some getting used to. I assume it'll error out and say method my not found? Thanks all, -Andy
multi return values
Hi all, what's wrong with this code: use v6; sub multireturn($x, $y) { my $a = $x * 2; my $b = $y * 2; return($a, $b); } my($a, $b) = multireturn(2, 3); using: This is Rakudo Perl 6, revision 32970 built on parrot 0.8.1-devel for i486-linux-thread-multi. I get: Method 'lvalue' not found for invocant of class 'PAST;Stmts' current instr.: 'parrot;PAST;Compiler;as_post' pc 2924 (src/PAST/Compiler.pir:742) called from Sub 'parrot;PAST;Compiler;post_children' pc 1783 (src/PAST/Compiler.pir:368) called from Sub 'parrot;PAST;Compiler;as_post' pc 2060 (src/PAST/Compiler.pir:500) called from Sub 'parrot;PAST;Compiler;post_children' pc 1783 (src/PAST/Compiler.pir:368) called from Sub 'parrot;PAST;Compiler;pirop' pc 3061 (src/PAST/Compiler.pir:796) called from Sub 'parrot;PAST;Compiler;post_children' pc 1783 (src/PAST/Compiler.pir:368) called from Sub 'parrot;PAST;Compiler;as_post' pc 2408 (src/PAST/Compiler.pir:614) called from Sub 'parrot;PCT;HLLCompiler;compile' pc 434 (src/PCT/HLLCompiler.pir:303) called from Sub 'parrot;PCT;HLLCompiler;eval' pc 868 (src/PCT/HLLCompiler.pir:502) called from Sub 'parrot;PCT;HLLCompiler;evalfiles' pc 1233 (src/PCT/HLLCompiler.pir:676) called from Sub 'parrot;PCT;HLLCompiler;command_line' pc 1412 (src/PCT/HLLCompiler.pir:765) called from Sub 'parrot;Perl6;Compiler;main' pc 16160 (perl6.pir:168)
whats wrong with this code?
Hi List -- I'v started playing around with perl 6, and I am having problems with this example: use v6; sub xsum (@list) { my $i = 0; print summing: ; for @list { $i += $_; print $_,,; } say = $i; return $i; } say sum = , xsum( (1,2,3,4,5) ); It returns this: summing: 1 2 3 4 5, = -1.2289e+09 sum = -1.2289e+09 Looks like the for @list has one element in it, 1 2 3 4 5. I'v tried: say sum = , xsum( (1,2,3,4,5) ); and say sum = , xsum( [1,2,3,4,5] ); and my @x = (1,2,3,4,5); say sum = , xsum( @x ); I'm using: This is Rakudo Perl 6, revision 30434 built on parrot 0.7.0-devel Thanks, -Andy
Re: whats wrong with this code?
Moritz Lenz wrote: Hi Andy, you seem to have discovered a whole bunch of bugs at once :/ Andy Colson wrote: Hi List -- I'v started playing around with perl 6, and I am having problems with this example: use v6; sub xsum (@list) { my $i = 0; print summing: ; for @list { $i += $_; print $_,,; } say = $i; return $i; } say sum = , xsum( (1,2,3,4,5) ); It returns this: summing: 1 2 3 4 5, = -1.2289e+09 sum = -1.2289e+09 Looks like the for @list has one element in it, 1 2 3 4 5. Not quite. When you add these lines: say @list.elems; say @list.WHAT at the start of the sub, you'll get 5 as the answer for the count, which seems correct, and 'List' as the type, which seems weird, but I'm not sure if it's actually wrong (normally something that starts with an @ should be an Array, but since it's a subroutine argument it's read only. So List might be fine). The 'for' in conjunction with this weird List is broken. This works: my @list = (1, 2, 3, 4, 5); my @x = @list; for @x { .say } (prints all numbers on separate lines) but this is broken: sub a(@b) { for @b { .say } }; my @x = (1, 2, 3); a(@x) prints all at once. The second problem is the result of the += operation, which does something really weird. When you write it as $i = $i + 1, it gives you 5, which is the number of items in the Array $i (which is correct, if we accept the previous bug for the moment) I'll write bug reports for both problems to [EMAIL PROTECTED] The recommended way to write such a sub is sub xsum([EMAIL PROTECTED]) { ... } xsum(1, 2, 3); With the * before the @list it is slurpy, which means that it doesn't expect one list as the argument, but rather an arbitrary number of items. If you happen to have an Array already, you can interpolate it: my @x = 1, 2, 3; xsum(|@x); HTH, Moritz Cool, thanks for the help there. The recommended way to write such a sub is sub xsum([EMAIL PROTECTED]) { ... } xsum(|@x); Ahh, but, if I already had a list, would that flatten and then rebuild the list, correct? (and would, effectively, make a copy of the list?) (vs. just passing the list?) (just passing a list like that passes a reference, right? and wont make a new copy?) Thanks, -Andy