> 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. So you need to slice into a
list, which in this case is done like,
perl -Mstrict -w -e 'print ((split("\\.", "a.b.c"))[0]);'
Notice the extra set of parens, otherwise you get a syntax error because
C<print> would otherwise use the first set as an argument list.
HTH,
http://danconia.org
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>