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.

nath
  • 2,848
  • 12
  • 45
  • 75
  • 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. – tchrist Nov 18 '10 at 15:51
  • Instead of index() use rindex() :D – Jassi Oct 04 '12 at 07:55

3 Answers3

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};
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
  • 1
    +1 for `File::Basename`. A regex is fragile and non-portable. – Alex Reynolds Nov 18 '10 at 15:42
  • 1
    Since when are filenames `\w` characters? Just in that example, right, not generally? – tchrist Nov 18 '10 at 15:55
  • @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 Ünür Nov 18 '10 at 16:10
  • 2
    @Sinan: Yeah well, I’ve been [overachieving on regexes](http://stackoverflow.com/questions/4213800/is-there-something-like-a-counter-variable-in-regular-expression-replace/4214173#4214173) 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. – tchrist Nov 18 '10 at 16:13
  • @thcrist I am afraid overachieving is an understatement. ;-) – Sinan Ünür Nov 18 '10 at 16:41
13

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.

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
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. ☺

tchrist
  • 78,834
  • 30
  • 123
  • 180