Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Using Split function.

by vinoth.ree (Monsignor)
on Aug 04, 2009 at 03:27 UTC ( #785632=perlquestion: print w/replies, xml ) Need Help??
vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

Yesterday I was modifiying my code to get a file name from the given path, I used the split function and separated by '/' character then got only the file name from the path, for this I used the temporary array and got the file name by the

arrayname[$#arrayname]. The problem is the path may differ according to the report I save, So I do not know the exact position of the filename. If is now the filename position, I will use the,

$filename_save=(split '/', $file_path)[File_name_position],

So I get the file name without using the temp array, How to find the file name without using the temp array in split or any other easy way to find the filename only.


Html tags were updated

Replies are listed 'Best First'.
Re: Using Split function.
by ig (Vicar) on Aug 04, 2009 at 03:31 UTC
    my $path = "/path/ending/with/a/filename"; my $file = (split(/\//,$path))[-1]; print "$file\n";


    use File::Spec; my $fullpath = "/path/ending/with/a/filename"; my ($volume, $path, $file) = File::Spec->splitpath($fullpath); print "$file\n";

      Thanks!!, I just remember this method by your post, Thank you so much. I will take first one instead of using the module for that single operation.

Re: Using Split function.
by jwkrahn (Monsignor) on Aug 04, 2009 at 03:36 UTC
    use File::Basename; my $filename_save = basename $file_path;
Re: Using Split function.
by Skeeve (Vicar) on Aug 04, 2009 at 06:46 UTC

    Using File::Basename is of course better, But to show a regular expression that could handle this as well:

    (my $filename_save= $file_path)=~ s#.*/##;

Re: Using Split function.
by ack (Deacon) on Aug 04, 2009 at 05:30 UTC

    The approach that I was thinking was similar to ig's but used the $#array construct rather than the index -1 that ig used. I like ig's much better as it shortens the construct to a single line of code to get the answer.

    My approach suggestion was:

    my @separated = split(/\//,$path); my $answer = $separated[$#separated];

    However, as I noted, ig's solution is much more elegant and efficient.

    ack Albuquerque, NM

      Did you read my question? Understood clearly?

      In my question,I told that I was using the temporary array and using the $#array_name to get the last element from the array. You also side the same thing.


      Spelling mistakes corrected

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://785632]
Approved by ig
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2018-06-18 19:38 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (110 votes). Check out past polls.