Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Using Split function.

by vinoth.ree (Parson)
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.

Update

Html tags were updated

Comment on Using Split function.
Select or Download Code
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";

    OR

    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 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
      ack,

      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.

      Update

      Spelling mistakes corrected

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#.*/##;

    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://785632]
Approved by ig
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2014-10-25 03:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (141 votes), past polls