8

I am new to perl scripting. Can some one tell me how to find the last indexof substring in a string which is repeat several times in the string.

Actully I want to extract the file name from a give path

 $outFile = "C:\\AOTITS\\BackOffice\\CSVFiles\\test.txt";

If I can find the last string of the '\' I cand extract the file name using substr function. I already did that in the following way. But it is inefficient.

$fragment =  $outFile ;
$count = index($fragment, "\\");
while($count > -1) {
    $fragment =  substr ($fragment, index($fragment, '\\')+1);
    $count = index($fragment, '\\');
 }

Can some one tell me a way to do that in a efficient way.

2
  • 1
    You really should be using "/" as the path separator, not "\\\\", because it makes it much harder to read and write. And the Micro$oft kernel doesn’t care. Commented Nov 18, 2010 at 15:51
  • Instead of index() use rindex() :D Commented Oct 4, 2012 at 7:55

3 Answers 3

15

Use File::Basename:

#!/usr/bin/env perl
use strict; use warnings;

use File::Basename;

my $outFile = "C:\\AOTITS\\BackOffice\\CSVFiles\\test.txt";

my ($name) = fileparse $outFile;
print $name, "\n";

NB: You can do this with regular expressions too, but when dealing with file names, use functions specifically designed to deal with file names. For completeness, here is an example of using a regular expression to capture the last part:

my ($name) = $outFile =~ m{\\(\w+\.\w{3})\z};
Sign up to request clarification or add additional context in comments.

5 Comments

+1 for File::Basename. A regex is fragile and non-portable.
Since when are filenames \w characters? Just in that example, right, not generally?
@tchrist: Of course, not generally. I did not want to put much effort into the regex, so just gave an example that worked in this case. Just a single solitary - would mess things up.
@Sinan: Yeah well, I’ve been overachieving on regexes lately. It’s cause I’m updating the regex chapter in the upcoming 4th Edition of Programming Perl, so it’s all in my head lately.
@thcrist I am afraid overachieving is an understatement. ;-)
14

Concerning the question in the title, you could use the rindex function:

  • rindex STR,SUBSTR,POSITION
  • rindex STR,SUBSTR

    Works just like index except that it returns the position of the last occurrence of SUBSTR in STR. If POSITION is specified, returns the last occurrence beginning at or before that position.

That said, it's better to parse file paths with File::Basename.

1 Comment

Thank you for providing the answer to the question in the title. This thread is the #1 hit in DDG when search for "perl last index of character". The thread would be less useful without an answer to the title.
4

Can someone tell me how to find the last index of s substring in a string which is repeated several times in the string?

Yes.

my $whole = "Can someone tell me how to find the last index of s substring in a string which is repeated several times in the string?";
my $piece = "string";

my $place;
if ($whole =~ m { .* \Q$piece\E }gsx) {
    $place = pos($whole) - length($piece);
    print "Last found it at position $place\n";
} else {
    print "Couldn't find it\n";
}

But take Sinan’s answer, since he answered what you wanted to know, not what you asked. ☺

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.