Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Get last occurence of a character in a string

by theroninwins (Friar)
on Aug 06, 2008 at 12:38 UTC ( [id://702620]=perlquestion: print w/replies, xml ) Need Help??

theroninwins has asked for the wisdom of the Perl Monks concerning the following question:

Here is the Problem:
I have somehting like:
c:\folder1\folder2\my_file_name.com c:\folder\filename.bat
Those file names are stored in an array that is then used for some stuff like
foreach $file (@file_list) { # Nacheinander Öffnen der Dateien die Ü +berprüft werden sollen print "$file\n"; my $comment = 0; open(IN,"$file") || print "Can't open $file!\n\n"; push (@include_temp, $file); while (<IN>) { ###Auskommentierte Zeilen überspringen### if (/\%\{.*/){ if(/^@([A-Z][^T][^W][^(]+)\s*\(/i){ # Suche nach Funktionen + vor einem Kommentar in der gleichen Zeile $function = $1; $function =~ s/\s+$//; push(@function_temp, $function); $comment =1; next; }else { $comment =1; } }
this is just part of the code.... but here it comes:
the filenames are not shortened yet. I only want to have the filename itself not the folders and all that so
c:\folder1\filename.doc should only be filename.doc
I thought of just choping away everything from the last \ onwards but don't really know how to .
Any idea??
Thx

UPDATE:
Ok I got it to work with File::Spec
foreach $file (@file_list) { (my $volume,my $directories, my $filename) = File::Spec->splitpath( +$file ); print "$filename\n";

Replies are listed 'Best First'.
Re: Get last occurence of a character in a string
by Corion (Patriarch) on Aug 06, 2008 at 12:43 UTC
Re: Get last occurence of a character in a string
by Fletch (Bishop) on Aug 06, 2008 at 12:43 UTC

    XY Problem. What you really want is File::Basename which will do this (in a platform independent fashion, even).

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Get last occurence of a character in a string
by FunkyMonk (Chancellor) on Aug 06, 2008 at 12:48 UTC
    Please disregard. I misread the OPs question. This code does the opposite.
    my $file = 'c:\folder1\filename.doc'; $file =~ s{ # Substitue... \\ # a backslash [^\\]* # zero or more not-backslashes $ # at the end of string } {}x; # with nothing # allow comments & spaces in search-pattern print $file; # c:\folder1


    Unless I state otherwise, all my code runs with strict and warnings
      You're giving the opposite of what was asked for, and you should escape your backslashes (in the assignment)
      my $path = 'c:\\folder1\\filename.doc'; my ($file) = ($path =~ /([^\\]*)$/);
      (And yes, File::Basename is the way to go in this case)

      UPDATE: Corrected /([^//]*)$/ to /([^\\]*)$/, thx FunkyMonk

        You're giving the opposite of what was asked for
        Yes, you're right. I misread the OP's question.

        and you should escape your backslashes
        Sure?
        my ($file) = ($path =~ /([^//]*)$/);

        Unmatched [ in regex; marked by <-- HERE in m/([ <-- HERE ^/
Re: Get last occurence of a character in a string
by JadeNB (Chaplain) on Aug 06, 2008 at 19:32 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-19 16:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found