vic2608 has asked for the wisdom of the Perl Monks concerning the following question:
Hello, I'm embarrassed to ask such a trivial question, but I couldn't find anything searching Perlmonks or the Net. Consider a function that returns a real list (not a reference), and you are only interested in a particular element. Some standard modules behave like this, e.g. File::Spec->splitpath.
This does not work:
my $name = File::Spec->splitpath($path)[2];This works, but it's ugly:
my $name; { my @dummy = File::Spec->splitpath($path); $name = $dummy[2]; }
Running through an intermediate anonymous array seems unnecessarily complex to me:
my $name = [ File::Spec->splitpath($path) ]->[2];Many thanks for pointing me to a more elegant way!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to access directly an element of an array / list returned by a function?
by ikegami (Patriarch) on Aug 26, 2010 at 18:30 UTC | |
|
Re: How to access directly an element of an array / list returned by a function?
by shawnhcorey (Friar) on Aug 26, 2010 at 19:43 UTC | |
by stevieb (Canon) on Aug 27, 2010 at 04:46 UTC | |
|
Re: How to access directly an element of an array / list returned by a function?
by TomDLux (Vicar) on Aug 26, 2010 at 17:29 UTC | |
by stevieb (Canon) on Aug 26, 2010 at 17:42 UTC | |
by vic2608 (Initiate) on Aug 26, 2010 at 17:47 UTC | |
by Anonymous Monk on Aug 26, 2010 at 19:04 UTC |