Instead of writing:
@holder = split /\./,"hello.world";
print @holder[0];
is it possible to just do a one-liner to just get the first element of the split? Something like:
print (split /\./,"hello.world")[0]
I get the following error when I try the second example:
print (...) interpreted as function at test.pl line 3.
syntax error at test.pl line 3, near ")["
You should have tried your hunch. That’s how to do it.
You could use a list-context assignment that grabs the first field only.
To print it, use
or
The plus sign is there because of a syntactic ambiguity. It signals that everything following are arguments to
print. The perlfunc documentation onprintexplains.In the case above, I find the case with
+much easier to write and read. YMMV.