From: "Wiggins d Anconia" <[EMAIL PROTECTED]> > > This works and does what I want it to: > > > > perl -e '@x = split("\\.", "a.b.c"); print $x[0];' > > > > Why does not this work? > > perl -e 'print @{split("\\.", "a.b.c")}[0];' > > > > Is there a compact way to take a slice of a split (or other function > > that returns an array) without creating a temporary variable? > > > > Thanks, > > Siegfried > > > > Sometimes when working out this kind of detail it is helpful to make a > full script and activate strict/warnings. In the above case you get > the following, > > > perl -Mstrict -w -e 'print @{split("\\.", "a.b.c")}[0];' > Use of implicit split to @_ is deprecated at -e line 1. > Can't use string ("3") as an ARRAY ref while "strict refs" in use at > -e line 1. > > Essentially C<split> returns a list, the construct C<@{ }> is a way to > slice into a hash, which you don't have.
There'd have to be a name of a variable between the @ and the opening curly brace for that to be a hash slice. This way its an array dereference: @arr = (1,2,3); $rArr = [EMAIL PROTECTED]; @other = @{$rArr}; Of course in this case you do not need the braces. You'd use the @{} if the thing you need to dereference is more complex, for example if you need to dereference a function result. So print join(', ', @{function('returning', 'arrayref')}); would be correct, just like print join(', ', @{function('returning', 'arrayref')}[1,2,4,7]); If you'd want just one item from the referenced array you would of course use ${}[] instead: print ${function('returning', 'arrayref')}[2]; In this case the function doesn't return a reference, but a list so there is no point in trying to dereference anything :-) This is where the "Can't use string ("3") as an ARRAY ref while "strict refs" in use at -e line 1." message comes from. The split() is evaluated in scalar context and thus returns the number of elements found in the string. And then the code tries to do this: perl -e 'print @{3}[0];' Jenda ===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz ===== When it comes to wine, women and song, wizards are allowed to get drunk and croon as much as they like. -- Terry Pratchett in Sourcery -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>