Substitute _last_ occurrece in string? 
Author Message
 Substitute _last_ occurrece in string?

How can I substitute the last occurence of a certain text string within
a string?

For instance, if I have the string $string = "abcdefghiajkl", I'd
like to be able to substitute the last "a" in $string with null so that
I'm returned "abcdefghijkl".

Is there an argument I can pass to the usual substitute
operator? For example:

$string =~ s/a//<ARGUMENT>

or is there a more lengthy way to do it? Thanks,

P.



Sun, 11 Jan 2004 22:00:23 GMT  
 Substitute _last_ occurrece in string?

Quote:

> How can I substitute the last occurence of a certain text string within
> a string?

> For instance, if I have the string $string = "abcdefghiajkl", I'd
> like to be able to substitute the last "a" in $string with null so that
> I'm returned "abcdefghijkl".

use a zero-width negative look-ahead:

$string = "abcadefaghiajkl";
$string =~ s/a(?!.*a)//;
print $string. "\n";

# prints abcadefaghijkl

cheers,
stefan



Mon, 12 Jan 2004 00:11:43 GMT  
 Substitute _last_ occurrece in string?

Quote:

> How can I substitute the last occurence of a certain text string within
> a string?
> For instance, if I have the string $string = "abcdefghiajkl", I'd
> like to be able to substitute the last "a" in $string with null so that
> I'm returned "abcdefghijkl".
> Is there an argument I can pass to the usual substitute
> operator? For example:

(snipped)

Use of index () and substr () will prove to be
very efficient for this task.

if (index ($string, $remove) > -1)

This line checks for the existence of
a character or sub-string to be removed.

$check = index ($string, $remove);
$check2 = rindex ($string, $remove);
if ($check2 > $check)

Those three lines check for more than
one instance of what is to be removed.
My substr () following those lines
performs the actual substitution.

Use of a purely regex method which performs
the same functions will invoke a memory wasteful
regex engine whether it is needed or not.
This is quite inefficient.

Godzilla!
--

TEST SCRIPT:
____________

#!perl

print "Content-type: text/plain\n\n";

$string = "abcdefghiajkl";
$remove = "a";

if (index ($string, $remove) > -1)
 {
  $check = index ($string, $remove);
  $check2 = rindex ($string, $remove);
  if ($check2 > $check)
   { substr ($string, $check2, length ($remove), ""); }
 }

print $string;

exit;

PRINTED RESULTS:
________________

abcdefghijkl



Mon, 12 Jan 2004 00:33:57 GMT  
 Substitute _last_ occurrece in string?

Quote:


> > How can I substitute the last occurence of a certain text string within
> > a string?

> > For instance, if I have the string $string = "abcdefghiajkl", I'd
> > like to be able to substitute the last "a" in $string with null so that
> > I'm returned "abcdefghijkl".

> use a zero-width negative look-ahead:

> $string = "abcadefaghiajkl";
> $string =~ s/a(?!.*a)//;
> print $string. "\n";

> # prints abcadefaghijkl

just for fun, you could also take advantage of the RE engine's right to
left operation:

$rev = reverse($string);
$rev =~ s/a//;
$string = reverse($rev);

hth-
--



Mon, 12 Jan 2004 01:04:21 GMT  
 Substitute _last_ occurrece in string?

Quote:

> Stefan Weiss"

> > > How can I substitute the last occurence of a certain text string within
> > > a string?
> > > For instance, if I have the string $string = "abcdefghiajkl", I'd
> > > like to be able to substitute the last "a" in $string with null so that
> > > I'm returned "abcdefghijkl".

(snipped)

Quote:
> just for fun, you could also take advantage of the RE engine's right to
> left operation:
> $rev = reverse($string);
> $rev =~ s/a//;
> $string = reverse($rev);

Will this work correctly for a single instance of
letter "a" within string? This is, a string only
contains one letter a and parameters are to remove
the "last instance" of this letter. Think on this.

Godzilla!



Mon, 12 Jan 2004 01:23:39 GMT  
 Substitute _last_ occurrece in string?
: How can I substitute the last occurence of a certain text string within
: a string?
:
: For instance, if I have the string $string = "abcdefghiajkl", I'd
: like to be able to substitute the last "a" in $string with null so that
: I'm returned "abcdefghijkl".

I typically use negative lookahead:

  s/a(?!.*a)//;

--
   |   Craig Berry - http://www.cinenet.net/~cberry/
 --*--  "Brute force done fast enough looks slick."
   |             - William Purves



Mon, 12 Jan 2004 01:33:17 GMT  
 Substitute _last_ occurrece in string?

Quote:

> How can I substitute the last occurence of a certain text string within
> a string?

> For instance, if I have the string $string = "abcdefghiajkl", I'd
> like to be able to substitute the last "a" in $string with null so that
> I'm returned "abcdefghijkl".

> Is there an argument I can pass to the usual substitute
> operator? For example:

> $string =~ s/a//<ARGUMENT>

> or is there a more lengthy way to do it? Thanks,

You can use the fact that regular expressions are greedy.

$string =~ s/(.*)a/$1/;

Or you can use the normal string functions

substr( $string, rindex( $string, 'a' ), 1 ) = '';

John
--
use Perl;
program
fulfillment



Mon, 12 Jan 2004 04:14:56 GMT  
 Substitute _last_ occurrece in string?

Quote:


..
> > just for fun, you could also take advantage of the RE engine's right to
> > left operation:

> > $rev = reverse($string);
> > $rev =~ s/a//;
> > $string = reverse($rev);

[ Michael: This is the first thing that came to my mind when I read
the question. I'm actually quite fond of Sexegers [1], but then I
figured the lookahead assertion would be cleaner in this case. ]

Quote:
> Will this work correctly for a single instance of
> letter "a" within string? This is, a string only
> contains one letter a and parameters are to remove
> the "last instance" of this letter.

It will remove the last instance of the letter 'a', which also
happens to be the only one. Why didn't you try it yourself?

BTW, the "solution" you posted elsewhere in this thread will _not_
work in this case. I find it amusing that you think it inefficient to
"invoke a memory wasteful regex engine" (which would solve the problem
in no more than 24 characters), then go ahead and propose an 8-liner
that reads like BASIC, does not even work correctly, and would only
waste the OP's time and patience. Efficient?

Quote:
> Think on this.

Indeed.

cheers,
stefan

[1] http://www.crusoe.net/~jeffp/sexeger/sexeger.html



Mon, 12 Jan 2004 04:57:16 GMT  
 Substitute _last_ occurrece in string?

(snipped)

Quote:
> > > just for fun, you could also take advantage of the RE engine's right to
> > > left operation:
> > > $rev = reverse($string);
> > > $rev =~ s/a//;
> > > $string = reverse($rev);
> > Will this work correctly for a single instance of
> > letter "a" within string? This is, a string only
> > contains one letter a and parameters are to remove
> > the "last instance" of this letter.
> It will remove the last instance of the letter 'a', which also
> happens to be the only one. Why didn't you try it yourself?

Besides being a lame brain troll, it is clear you are yet another
proud graduate of the Sears, Roebuck & Co. Academy Of Language Arts.

Godzilla!



Mon, 12 Jan 2004 05:07:40 GMT  
 Substitute _last_ occurrece in string?

(snipped)

Quote:
> [ Michael: This is the first thing that came to my mind when I read
> the question. I'm actually quite fond of Sexegers [1], but then I
> figured the lookahead assertion would be cleaner in this case. ]
> BTW, the "solution" you posted elsewhere in this thread will _not_
> work in this case. I find it amusing that you think it inefficient to
> "invoke a memory wasteful regex engine" (which would solve the problem
> in no more than 24 characters), then go ahead and propose an 8-liner
> that reads like BASIC, does not even work correctly, and would only
> waste the OP's time and patience. Efficient?

Would you like salt and pepper with your crow or,
would you simply prefer cheese with your whine?

Godzilla!
--

#!perl

print "Content-type: text/plain\n\n";

use Benchmark;

print "Run One:\n\n";
&Time;

print "\n\nRun Two:\n\n";
&Time;

print "\n\nRun Three:\n\n";
&Time;

sub Time
 {
  timethese (1000000,
  {
'name1' =>
'$string = "Godzilla Rocks! Godzilla Rocks! Godzilla Rocks!
 Godzilla Rocks! Godzilla Rocks! Godzilla Rocks! Godzilla Rocks!";
 if (rindex ($string, "Godzilla Rocks!") > index ($string, "Godzilla Rocks!"))
  { substr ($string, rindex ($string, "Godzilla Rocks!"), length ("Godzilla Rocks!"), ""); }',

'name2' =>
'$string = "Godzilla Rocks! Godzilla Rocks! Godzilla Rocks!
 Godzilla Rocks! Godzilla Rocks! Godzilla Rocks! Godzilla Rocks!";
 $string =~ s/Godzilla Rocks!(?!.*Godzilla Rocks!)//s;',

  } );

 }

exit;

PRINTED RESULTS:
________________

Run One:

Benchmark: timing 1000000 iterations of name1, name2...


Run Two:

Benchmark: timing 1000000 iterations of name1, name2...


Run Three:

Benchmark: timing 1000000 iterations of name1, name2...




Mon, 12 Jan 2004 06:53:41 GMT  
 Substitute _last_ occurrece in string?

(snipped)

Quote:
> BTW, the "solution" you posted elsewhere in this thread will _not_
> work in this case. I find it amusing that you think it inefficient to
> "invoke a memory wasteful regex engine" (which would solve the problem
> in no more than 24 characters), then go ahead and propose an 8-liner
> that reads like BASIC, does not even work correctly, and would only
> waste the OP's time and patience. Efficient?

Here is a second serving of crow for you to eat.

My presumption is you have not noticed your impotent
Sexeger does not print correctly.

Clearly, my code is quite potent. I contribute this
to both a lack of a {*filter*} and lack of a {*filter*} mounted
atop my shoulders.

Godzilla!   Queen Of {*filter*}less.
--

#!perl

print "Content-type: text/plain\n\n";

use Benchmark;

print "Run One:\n\n";
&Time;

print "\n\nRun Two:\n\n";
&Time;

print "\n\nRun Three:\n\n";
&Time;

sub Time
 {
  timethese (1000000,
  {
   'name1' =>
   '$string = "Godzilla Rocks! Godzilla Rocks! Godzilla Rocks!
    Godzilla Rocks! Godzilla Rocks! Godzilla Rocks! Godzilla Rocks!";
    substr ($string, rindex ($string, "Godzilla Rocks!"), length ("Godzilla Rocks!"), "");',

   'name2' =>
   '$string = "Godzilla Rocks! Godzilla Rocks! Godzilla Rocks!
    Godzilla Rocks! Godzilla Rocks! Godzilla Rocks! Godzilla Rocks!";
    $rev = reverse($string);
    $rev =~ s/Godzilla Rocks!//s;
    $string = reverse($rev);',

  } );

 }

exit;

PRINTED RESULTS:
________________

Run One:

Benchmark: timing 1000000 iterations of name1, name2...


Run Two:

Benchmark: timing 1000000 iterations of name1, name2...


Run Three:

Benchmark: timing 1000000 iterations of name1, name2...




Mon, 12 Jan 2004 07:15:23 GMT  
 Substitute _last_ occurrece in string?

Quote:

> Here is a second serving of crow for you to eat.

> My presumption is you have not noticed your impotent
> Sexeger does not print correctly.

> Clearly, my code is quite potent. I contribute this
> to both a lack of a {*filter*} and lack of a {*filter*} mounted
> atop my shoulders.

[phoney benchmarks skipped]

I knew I should not have responded to any of your postings. Usually your
posts just get filtered by my mail client, but this time you posted in a
thread I had marked 'watched'. Guess I'll have to fix the priorities of
my filters...

I will not comment on your benchmarks, please see my previous posting for
the reason why. And rest assured I will not comment on any of your
postings again - some people just take a bit longer to learn a lesson...

And now I'd better stop and set a f'up2p before you accuse me of being
10 persons at once, and that I originally posted the question just to
answer it myself.

Be happy with your code.

cheers,
stefan

PS: just for fun, try http://www.*-*-*.com/ %20manure



Mon, 12 Jan 2004 08:20:11 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Substituting last instance

2. split and substitute, substitute, substitute

3. Substituting many strings in one pass over the input

4. Substituting many sparsely distrbuted strings in many files

5. substituting strings in source trees?

6. Pattern Match - substitute a string after the match

7. Repeat string matching/substituting ?

8. substituting string of text

9. substitute a string in array

10. how to substitute letters within a string

11. substituting keywords in a string

12. how to substitute strings in a newly created file

 

 
Powered by phpBB® Forum Software