how to substitute letters within a string 
Author Message
 how to substitute letters within a string

Hi,
I wish to substitute any occurrences of an uppercase I in a variable
with the lower case i. I am familiar with using:

s/I/i/g;

but this operates on the standard input $_

whereas I have a variable (  $fred)   who's value is a string with
possible occurrences of "I" within it. SO, how do I get the substitute
command to work on $fred ??

Any ideas?

Thanks

Paul

--
To respond to this posting, remove -nospam- from my email address.
Sorry for the inconvenience



Sat, 28 Feb 2004 03:59:52 GMT  
 how to substitute letters within a string

Quote:
> Hi,
> I wish to substitute any occurrences of an uppercase I in a variable
> with the lower case i. I am familiar with using:

> s/I/i/g;

> but this operates on the standard input $_

> whereas I have a variable (  $fred)   who's value is a string with
> possible occurrences of "I" within it. SO, how do I get the substitute
> command to work on $fred ??

$fred =~ s/I/i/g;

See `perldoc perlop` under 'Binding Operators'.

--
Me? Lady, I'm your worst nightmare -- a pumpkin with a gun.
    -- Mervyn Pumpkinhead, The Sandman



Sat, 28 Feb 2004 04:01:45 GMT  
 how to substitute letters within a string

Quote:

> I wish to substitute any occurrences of an uppercase I in a variable
> with the lower case i. I am familiar with using:
> s/I/i/g;
> but this operates on the standard input $_
> whereas I have a variable (  $fred)   who's value is a string with
> possible occurrences of "I" within it. SO, how do I get the substitute
> command to work on $fred ??

$fred =~ tr/I/i/;

Godzilla!
--

Looking up [pauls.seanet.com]

Server:  krim.seanet.com
Address:  199.181.164.11

*** krim.seanet.com can't find pauls.seanet.com: No response from server

[End Query]

Error accessing name server.
Reasons may include:
Invalid domain name.
MX records not found for this domain.
Name server not responding correctly or off-line.



Sat, 28 Feb 2004 04:09:48 GMT  
 how to substitute letters within a string

(snipped)

Quote:
> > I wish to substitute any occurrences of an uppercase I in a variable
> > with the lower case i. I am familiar with using:
> > s/I/i/g;
> $fred =~ tr/I/i/;

Godzilla!
--

GODZILLA DOS BENCHMARK:
_______________________

test1.pl
________

#!perl

$godzilla = "GodzIlla Is an Igenious InventIve InnovatIve Perl Programmer";
$godzilla =~ tr/I/i/;
exit;

test2.pl
________

#!perl

$godzilla = "GodzIlla Is an Igenious InventIve InnovatIve Perl Programmer";
$godzilla =~ s/I/i/g;
exit;

PRINTED RESULTS:
________________

C:\APACHE\USERS\TEST>godzilla -N10 perl test1.pl
Average Time: 50 ms

C:\APACHE\USERS\TEST>godzilla -N10 perl test2.pl
Average Time: 77 ms

BENCHMARK TESTING:
__________________

#!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' =>
   '$godzilla = "GodzIlla Is an IgenIous InventIve InnovatIve Perl Programmer";
    $godzilla =~ tr/I/i/;',

   'name2' =>
   '$godzilla = "GodzIlla Is an IgenIous InventIve InnovatIve Perl Programmer";
    $godzilla =~ s/I/i/g;',
  } );
 }

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...




Sat, 28 Feb 2004 04:36:20 GMT  
 how to substitute letters within a string

Quote:

> I wish to substitute any occurrences of an uppercase I in a variable
> with the lower case i. I am familiar with using:
> s/I/i/g;
> but this operates on the standard input $_
> whereas I have a variable (  $fred)   who's value is a string with
> possible occurrences of "I" within it. SO, how do I get the substitute
> command to work on $fred ??

forget s/// in this case.
  $fred =~ tr/I/i/;
hth, tina

--
http://www.tinita.de \  enter__| |__the___ _ _ ___
tina's moviedatabase  \     / _` / _ \/ _ \ '_(_-< of
search & add comments  \    \ _,_\ __/\ __/_| /__/ perception



Sat, 28 Feb 2004 15:04:10 GMT  
 how to substitute letters within a string

Quote:

> Hi,
> I wish to substitute any occurrences of an uppercase I in a variable
> with the lower case i. I am familiar with using:

> s/I/i/g;

> but this operates on the standard input $_

> whereas I have a variable (  $fred)   who's value is a string with
> possible occurrences of "I" within it. SO, how do I get the substitute
> command to work on $fred ??

> Any ideas?

> Thanks

> Paul

> --
> To respond to this posting, remove -nospam- from my email address.
> Sorry for the inconvenience

Hi Everyone,
Thanks for your help!!

Paul

--
To respond to this posting, remove -nospam- from my email address.
Sorry for the inconvenience



Sat, 28 Feb 2004 16:22:07 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. split and substitute, substitute, substitute

2. Substituting Only Within Matched Substring

3. Finding a string within a string

4. Recognizing first letter in a string

5. check if a string has any capital letters?

6. Getting the First Letter in string

7. Regular Expression: Remove Double Letters from a String?

8. Get first letter of string?

9. UpperCase first letter of string only.

10. PERLFUNC: lcfirst - return a string with just the next letter in lower case

11. PERLFUNC: ucfirst - return a string with just the next letter in upper case

12. PERLFUNC: lcfirst - return a string with just the next letter in lower case

 

 
Powered by phpBB® Forum Software