You can try:

$path = 'd:/ab/c/de/fgh/file.txt';

my $filename = (split(/\//,$path))[-1];
printf "%-s\n", $filename;

which prints

file.txt

Wags ;)
-----Original Message-----
From: Timothy Johnson [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, April 03, 2002 22:14
To: 'Michael Kelly '; [EMAIL PROTECTED]
Subject: RE: Returning one item out of a list


 
I think what you're looking for is something like this:

my $filename = split(/\//,$path)[7]; #assuming you want the seventh item

I don't know how you would get the last one if you didn't know how many
there were, though.

Disclaimer:  I haven't tested this.  Use at your own risk.  Do not expose to
sunlight.  Do not use in excessive cold or heat.  Do not use if you are
taking MAOI's, a particular type of medicine that is sometimes prescribed
for depression.  Use of this code is discouraged while driving or using
heavy machinery.  Do not use without adult supervision.  The code being
attempted in the program you are now watching is being performed by a
professional.  Do not attempt this in your own home.  The owners and
creators of the preceeding code accept no liability for the use of their
product, and make no guarantees, including but not limited to its fitness
for a particular purpose, or the fitness of the mind of the programmer.
Please remember to spay or neuter your pets.




-----Original Message-----
From: Michael Kelly
To: [EMAIL PROTECTED]
Sent: 4/3/02 9:17 PM
Subject: Returning one item out of a list

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.

Thanks,
-- 
Michael


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


--------------------------------------------------------------------------------
This email may contain confidential and privileged 
material for the sole use of the intended recipient. 
If you are not the intended recipient, please contact 
the sender and delete all copies.

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

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

Reply via email to