At 09:17 PM 4/3/02 -0800, Michael Kelly wrote:
>Hey all,
>
>I seem to remember seeing this done somewhere, but now I can't figure out
>how to do it now.
>
>If you have a function that returns a list, such as split(), is there a way
>to only return one item from that list, as if you were working with an
>array?
>
>For instance, if you have something like this:
>
>     my $path = '/big/long/path/to/something.txt';
>
>     my @dirs = split(/\//,$path);
>
>     my $filename = $dirs[$#dirs];
>
>Is there a way to eliminate the storing of each directory in @dirs, and just
>assign $filename straight to the last item of what split() returns? I'm not
>sure if it can be done, and if it can, I'm probably overlooking something
>obvious... either way, I can't figure it out.

Lists can be subscripted:

         my $filename = (split/\//,$path)[-1];

Another way to do it:

         my ($filename) = $path =~ m!.*/(.*)!);

And a filesystem-independent way uses File::Basename.

--
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to