Substitution problem

2005-09-06 Thread Moon, John
Does any one know how to do this with just a substitution?

perl -e '$a=q{Data.m1234.D1234567890};
$a =~/\d+$/;
$numbers = q{#} x length($&);
$a=~ s/\d+$/$numbers/; print "$a\n";'

What " Data.m1234.D## " as a result.

John W Moon

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




substitution problem

2003-10-23 Thread Steve Massey
Hi

I though I had sussed this s/ stuff but

#! /usr/bin/perl -w

$test =  "BRIGHTON (Firm)";


print "$test\n";
$test =~ s/,*/,/;
$test =~ s/,*$/,/g;

print "$test\n";



does not work, I want to substitute all multiple commas into a single one.

any help appreciated

Steve

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Substitution Problem

2001-06-08 Thread Mark Martin

Hi,
I'm taking in a csv file and splitting it. One of the fields(doc) could
possibly have six spaces which will disrupt the program later on so I need
to substitute in dummy values. $doc == "  " finds the "empty" fields
okay but the substitution doesn't work. Clear as Mud?? Any ideas?

open (FILEHANDLE,$file);


while (){

chomp;

($type,$doc,$narrative,$amount,$bdate,$branch) = split(/,/);

if ($doc == "  "){

doc =~ s/  /11/;
_

Mark Martin
Computer Centre
National University of Ireland Maynooth

Tel: (01)708 4716/3830
Fax: (01)628 6249



substitution problem

2001-05-29 Thread prachi shroff

Hi again!

I am having problems using the "s///" operator for substitution in of a 
particular pattern in a specified file.
For eg.

open(TRY,"+)
{
  $_ =~ s/a/p/g;
}

Is this the right way of doing it? And with what access mode shall I open 
the file ( >> or +> or +< ) ? And is the while loop
fine? What happens with this code is this code itself gets written in 
"try.txt"


Thanks,
Prachi
_
Get your FREE download of MSN Explorer at http://explorer.msn.com




Re: Substitution Problem

2010-06-28 Thread Jimstone77

can anyone give me some help on a perl program to change a file of  mine. 
The string is:
 
$_ = "from ''alam' (481) or possibly  (in the sense of dumbness);  
solitary; Ulam, the (6155) name of two Soldiers;--Ulam."
 
Where each line is $_
I'm trying to substitute a hyperlink for each  NUMBER that is in 
parenthesis only, while not touching the words in parenthesis  or any other 
numbers.
For example, I want to change (481) in the line above  to 
(481) and (6155) to  
(6155) and any 
others without changing  anything in (in the sense of dumbness).
 
Doing the same thing with each line in a file. Should I use a substitution? 
 Can anyone show me some code to accomplish this.  Thanks.


Re: Substitution Problem

2010-06-28 Thread John W. Krahn

jimston...@aol.com wrote:


can anyone give me some help on a perl program to change a file of  mine.
The string is:

$_ = "from ''alam' (481) or possibly  (in the sense of dumbness);
solitary; Ulam, the (6155) name of two Soldiers;--Ulam."

Where each line is $_
I'm trying to substitute a hyperlink for each  NUMBER that is in
parenthesis only, while not touching the words in parenthesis  or any other 
numbers.
For example, I want to change (481) in the line above  to
(481) and (6155) to  
(6155) and any
others without changing  anything in (in the sense of dumbness).


s/\((\d+)\)/($1)/g;



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Substitution Problem

2010-06-28 Thread John W. Krahn

jimston...@aol.com wrote:


can anyone give me some help on a perl program to change a file of  mine.
The string is:

$_ = "from ''alam' (481) or possibly  (in the sense of dumbness);
solitary; Ulam, the (6155) name of two Soldiers;--Ulam."

Where each line is $_
I'm trying to substitute a hyperlink for each  NUMBER that is in
parenthesis only, while not touching the words in parenthesis  or any other 
numbers.
For example, I want to change (481) in the line above  to
(481) and (6155) to  
(6155) and any
others without changing  anything in (in the sense of dumbness).


s/\((\d+)\)/($1<\/a>)/g;



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Substitution Problem

2010-06-28 Thread Jimstone77
 
In a message dated 6/28/2010 7:49:47 P.M. Eastern Daylight Time,  
jwkr...@shaw.ca writes:

jimston...@aol.com wrote:
>
> can anyone give me some  help on a perl program to change a file of  mine.
> The string  is:
>
> $_ = "from ''alam' (481) or possibly  (in the sense  of dumbness);
> solitary; Ulam, the (6155) name of two  Soldiers;--Ulam."
>
> Where each line is $_
> I'm trying to  substitute a hyperlink for each  NUMBER that is in
> parenthesis  only, while not touching the words in parenthesis  or any 
other  numbers.
> For example, I want to change (481) in the line above   to
> (481

) and (6155) to   (6155) and any
> others without  changing  anything in (in the sense of dumbness).

s/\((\d+)\)/(_$1_ (mip://050adf60/mypage.php?$1) )/g; 

Excellent! Works perfectly. Thank you so much!
 
 


Re: Substitution Problem

2010-06-28 Thread Steve Bertrand
On 2010.06.28 20:39, jimston...@aol.com wrote:
>  
> In a message dated 6/28/2010 7:49:47 P.M. Eastern Daylight Time,  
> jwkr...@shaw.ca writes:
> 
> jimston...@aol.com wrote:
>>
>> can anyone give me some  help on a perl program to change a file of  mine.
>> The string  is:
>>
>> $_ = "from ''alam' (481) or possibly  (in the sense  of dumbness);
>> solitary; Ulam, the (6155) name of two  Soldiers;--Ulam."
>>
>> Where each line is $_
>> I'm trying to  substitute a hyperlink for each  NUMBER that is in
>> parenthesis  only, while not touching the words in parenthesis  or any 
> other  numbers.
>> For example, I want to change (481) in the line above   to
>> (481
> 
> ) and (6155) to   (6155) and any
>> others without  changing  anything in (in the sense of dumbness).
> 
> s/\((\d+)\)/(_$1_ (mip://050adf60/mypage.php?$1) )/g; 
> 
> Excellent! Works perfectly. Thank you so much!

Perhaps you could post the code you originally started with, so that
you/others can understand how/why John's hack works, particularly if
your data ever changes.

Steve

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Substitution Problem

2010-06-28 Thread Chas. Owens
On Mon, Jun 28, 2010 at 19:48, John W. Krahn  wrote:
snip
> s/\((\d+)\)/($1)/g;
snip

Since Perl 5.8.0, \d does not mean [0-9], it means any character that
is classified as a digit in Unicode.  In Perl 5.12.1, there are five
hundred seventy-seven characters that will match \d.  If it is your
intent to replace "᠔᠒" with "᠔᠒" then \d
is a good choice; however, if you want to replace digits you can do
math with, I would suggest using [0-9].

Note, Mongolian isn't the only problem, there is also "𝟺𝟸" which
looks like "42", but is really "\x{1d7fa}\x{1d7f8}".  If you want both
"\x{1d7fa}\x{1d7f8}" and "42" to point to the same page, you will need
to use some form of transliteration like [Unicode::Digits][1]:

use Unicode::Digits qw/digits_to_int/;

s{
\( (\d+) \)
}{
sprintf "(%s)", digits_to_int($1), $1
}xeg

 [1] : http://search.cpan.org/dist/Unicode-Digits/lib/Unicode/Digits.pm

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




RE: Substitution problem

2005-09-06 Thread Wagner, David --- Senior Programmer Analyst --- WGO
Moon, John wrote:
> Does any one know how to do this with just a substitution?
> 
> perl -e '$a=q{Data.m1234.D1234567890};
>   $a =~/\d+$/;
>   $numbers = q{#} x length($&);
>   $a=~ s/\d+$/$numbers/; print "$a\n";'
> 
> What " Data.m1234.D## " as a result.
> 
> John W Moon

Did the following:
perl -e '$a=q{Data.m1234.D1234567890};
$a =~ s/(\d+)$/'#'x length($1)/e;
print "$a\n";
output was:
Data.m1234.D##

Wags ;)


***
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
***


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Substitution problem

2005-09-06 Thread John W. Krahn
Moon, John wrote:
> Does any one know how to do this with just a substitution?
> 
> perl -e '$a=q{Data.m1234.D1234567890};
>   $a =~/\d+$/;
>   $numbers = q{#} x length($&);
>   $a=~ s/\d+$/$numbers/; print "$a\n";'
> 
> What " Data.m1234.D## " as a result.

$ perl -le'$_ = q{Data.m1234.D1234567890};
s/(\d+)$/ "#" x length $1 /e;
print;
'
Data.m1234.D##


$ perl -le'$_ = q{Data.m1234.D1234567890}; 1 while s/\d(#*)$/#$1/; print'
Data.m1234.D##


$ perl -le'$_ = q{Data.m1234.D1234567890}; 1 while s/\d(?=#*$)/#/; print'
Data.m1234.D##


$ perl -le'$_ = q{Data.m1234.D1234567890}; s/\d(?=\d+$|$)/#/g; print'
Data.m1234.D##



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Substitution problem

2005-09-06 Thread John W. Krahn
John W. Krahn wrote:
> Moon, John wrote:
>>Does any one know how to do this with just a substitution?
>>
>>perl -e '$a=q{Data.m1234.D1234567890};
>>  $a =~/\d+$/;
>>  $numbers = q{#} x length($&);
>>  $a=~ s/\d+$/$numbers/; print "$a\n";'
>>
>>What " Data.m1234.D## " as a result.
> 
> $ perl -le'$_ = q{Data.m1234.D1234567890};
> s/(\d+)$/ "#" x length $1 /e;
> print;
> '
> Data.m1234.D##
> 
> $ perl -le'$_ = q{Data.m1234.D1234567890}; 1 while s/\d(#*)$/#$1/; print'
> Data.m1234.D##
> 
> $ perl -le'$_ = q{Data.m1234.D1234567890}; 1 while s/\d(?=#*$)/#/; print'
> Data.m1234.D##
> 
> $ perl -le'$_ = q{Data.m1234.D1234567890}; s/\d(?=\d+$|$)/#/g; print'
> Data.m1234.D##

$ perl -le'$_ = q{Data.m1234.D1234567890}; s/\d(?=\d*$)/#/g; print'
Data.m1234.D##


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: substitution problem

2003-10-23 Thread Steve Massey
Ok scrub that

 + works but * doesn't

i'm guess the * is matching anything 

took me 2 hours to figure that  ;)






-Original Message-
From: Steve Massey [mailto:[EMAIL PROTECTED]
Sent: 23 October 2003 11:33
To: [EMAIL PROTECTED]
Subject: substitution problem


Hi

I though I had sussed this s/ stuff but

#! /usr/bin/perl -w

$test =  "BRIGHTON (Firm)";


print "$test\n";
$test =~ s/,*/,/;
$test =~ s/,*$/,/g;

print "$test\n";



does not work, I want to substitute all multiple commas into a single one.

any help appreciated

Steve

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: substitution problem

2003-10-23 Thread Gary Stainburn
On Thursday 23 Oct 2003 11:52 am, Steve Massey wrote:
> Ok scrub that
>
>  + works but * doesn't
>
> i'm guess the * is matching anything
>
> took me 2 hours to figure that  ;)
>


Hi Steve,

+ matches 1 or more,
* matches 0 or more.

for better efficiency, you really want to do 

s/,,+/,/;

which will not then replace 1 comma with another.

Gary

>
>
>
>
>
> -Original Message-
> From: Steve Massey [mailto:[EMAIL PROTECTED]
> Sent: 23 October 2003 11:33
> To: [EMAIL PROTECTED]
> Subject: substitution problem
>
>
> Hi
>
> I though I had sussed this s/ stuff but
>
> #! /usr/bin/perl -w
>
> $test =  "BRIGHTON (Firm)";
>
>
> print "$test\n";
> $test =~ s/,*/,/;
> $test =~ s/,*$/,/g;
>
> print "$test\n";
>
>
>
> does not work, I want to substitute all multiple commas into a single one.
>
> any help appreciated
>
> Steve

-- 
Gary Stainburn
 
This email does not contain private or confidential material as it
may be snooped on by interested government parties for unknown
and undisclosed purposes - Regulation of Investigatory Powers Act, 2000 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: substitution problem

2003-10-23 Thread Wiggins d Anconia
> Ok scrub that
> 
>  + works but * doesn't
> 
> i'm guess the * is matching anything 
> 
> took me 2 hours to figure that  ;)
> 
To de-mystify the regexes some have a look at:

perldoc perlretut
perldoc perlre

http://danconia.org

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: substitution problem

2003-10-23 Thread Mark Anderson
> From: Steve Massey [mailto:[EMAIL PROTECTED]
> Sent: 23 October 2003 11:33
>
> #! /usr/bin/perl -w
>
> $test =  "BRIGHTON (Firm)";
>
>
> print "$test\n";
> $test =~ s/,*/,/;
> $test =~ s/,*$/,/g;
>
> print "$test\n";

It looks like the +/* issue has been discussed.  The other option you could
use would be ,{2,} which matches 2 or more ,s in a row.

The other thing that I notices is that you are using the g option on the
wrong substitution.  It matches repeatedly within the string.  There will
only ever be one set of commas at the end of the string, but there may be
multiple sets within the string.  A single substitution of
$test =~ s/,+/,/g;
will replace the string at the end as well as any that show up in
mid-string.  The second substitution can then be changed if you don't want a
trailing comma, to
$test =~ s/,$//;

/\/\ark


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: substitution problem

2003-10-23 Thread R. Joseph Newton
Steve Massey wrote:

> Hi
>
> I though I had sussed this s/ stuff but
>
> #! /usr/bin/perl -w
>
> $test =  "BRIGHTON (Firm)";
>
> print "$test\n";
> $test =~ s/,*/,/;
> $test =~ s/,*$/,/g;
>
> print "$test\n";
>
> does not work, I want to substitute all multiple commas into a single one.
>
> any help appreciated
>
> Steve

One of these should work:

Greetings! E:\d_drive\perlStuff>perl -w
$test =  "BRIGHTON (Firm)";


print "$test\n";
$test =~ s/,+/,/g;
print "$test\n";
^Z
BRIGHTON (Firm)
BRIGHTON, (Firm),

Greetings! E:\d_drive\perlStuff>perl -w
$test =  "BRIGHTON (Firm)";

print "$test\n";
$test =~ s/,*/,/g;
$test =~ s/,*/BRIGHTON (Firm)
Terminating on signal SIGINT(2)

Greetings! E:\d_drive\perlStuff>perl -w
$test =  "BRIGHTON (Firm)";

print "$test\n";
$test =~ s/,+/,/g;
$test =~ s/,+$//;
print "$test\n";
^Z
BRIGHTON (Firm)
BRIGHTON, (Firm)

Greetings! E:\d_drive\perlStuff>

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: substitution problem

2003-11-06 Thread John W. Krahn
Steve Massey wrote:
> 
> Hi

Hello,

> I though I had sussed this s/ stuff but
> 
> #! /usr/bin/perl -w
> 
> $test =  "BRIGHTON (Firm)";
> 
> print "$test\n";
> $test =~ s/,*/,/;
> $test =~ s/,*$/,/g;
> 
> print "$test\n";
> 
> does not work, I want to substitute all multiple commas into a single one.

You should use tr/// instead of s///.  :-)

$test =~ tr/,//s;


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: Substitution Problem

2001-06-08 Thread Wagner-David

One thing is that you are doing a numeric compare(ie == vs eq) Perl tries it
best to come up with a valid response.  Since you are doing the compare, why
not just do the re as

$doc =~ s/^\s{6}$/11/;
which says if you have six whitespace then change to 1's or
whatever.

Wags ;)

-Original Message-
From: Mark Martin [mailto:[EMAIL PROTECTED]]
Sent: Friday, June 08, 2001 10:01
To: [EMAIL PROTECTED]
Subject: Substitution Problem


Hi,
I'm taking in a csv file and splitting it. One of the fields(doc) could
possibly have six spaces which will disrupt the program later on so I need
to substitute in dummy values. $doc == "  " finds the "empty" fields
okay but the substitution doesn't work. Clear as Mud?? Any ideas?

open (FILEHANDLE,$file);


while (){

chomp;

($type,$doc,$narrative,$amount,$bdate,$branch) =
split(/,/);

if ($doc == "  "){

doc =~ s/  /11/;
_

Mark Martin
Computer Centre
National University of Ireland Maynooth

Tel: (01)708 4716/3830
Fax: (01)628 6249



Re: Substitution Problem

2001-06-08 Thread Carl Rogers

Good day;
At 05:01 PM 6/8/2001 +, Mark Martin wrote:

>
> if ($doc == "  "){
>
> doc =~ s/  /11/;

should be:
$doc =~ s/  /11/;   #don't forget the "$"


Also, try
$doc =~ tr/  /11/;

Being a newbie myself, I stumbled across the realization that if 
substitution fails, try the translation (tr) and vice versa.
Don't know why it works sometimes, but it does.

Hope this works/helps.




RE: Substitution Problem

2001-06-08 Thread Dave Newton

Mart Marken said:
> I'm taking in a csv file and splitting it. One of the fields(doc) could
> possibly have six spaces which will disrupt the program later on so I need
> to substitute in dummy values. $doc == "  " finds the "empty" fields
> okay but the substitution doesn't work. Clear as Mud?? Any ideas?

Once I fixed an issue like this (because it suited the problem
well) by checking to see how many values I got back from the
split and doing things based on that number; perhaps that
would work for you.

Dave
--
Dave Newton, [EMAIL PROTECTED]




Re: Substitution Problem

2001-06-08 Thread M.W. Koskamp

> -Original Message-
> From: Mark Martin [mailto:[EMAIL PROTECTED]]
> Sent: Friday, June 08, 2001 10:01
> To: [EMAIL PROTECTED]
> Subject: Substitution Problem
>
>
> Hi,
> I'm taking in a csv file and splitting it. One of the fields(doc) could
> possibly have six spaces which will disrupt the program later on so I need
> to substitute in dummy values. $doc == "  " finds the "empty" fields
> okay but the substitution doesn't work. Clear as Mud?? Any ideas?
>
> open (FILEHANDLE,$file);
>
>
> while (){
>
> chomp;
>
> ($type,$doc,$narrative,$amount,$bdate,$branch) =
> split(/,/);
>
> if ($doc == "  "){
>
>  doc =~ s/  /11/;
First of all you should use eq instead of == when comparing strings.
You can also omit the if, or dont use a substitute.
What you are sayinf now is: If $doc equals some spaces, then search and
replace some spaces with some zero's.

is would try this:

$doc =~ s/\s\1\g unless $doc;

Maarten.




Re: substitution problem

2001-05-29 Thread J. J. Horner

Is there any reason you chose not to do it this way:

# From Perl Cookbook:

open(TRY,"+< try.txt")  or die "Couldn't open try.txt: $!\n";
my @lines =# Slurp all lines into an 
array.
foreach my $line (@lines) { # Iterate through lines here.
$line =~ s/windows/linux/g; # Modify lines here.
}
seek(TRY,0,0)   or die "Error seeking file: 
$!\n";
print TRY @linesor die "Error writing to file: 
$!\n";
truncate(TRY,tell(TRY)) or die "Error truncating file: $!\n";
close(TRY)  or die "Error closing 
file: $!\n";


It may not be as short and sweet, but it works, and it is easier to debug.

JJ



* prachi shroff ([EMAIL PROTECTED]) [010529 15:41]:
> Hi again!
> 
> I am having problems using the "s///" operator for substitution in of a 
> particular pattern in a specified file.
> For eg.
> 
> open(TRY,"+ while()
> {
>   $_ =~ s/a/p/g;
> }
> 
> Is this the right way of doing it? And with what access mode shall I open 
> the file ( >> or +> or +< ) ? And is the while loop
> fine? What happens with this code is this code itself gets written in 
> "try.txt"
> 
> 
> Thanks,
> Prachi
> _
> Get your FREE download of MSN Explorer at http://explorer.msn.com

-- 
J. J. Horner
"H*","6d6174686c696e40326a6e6574776f726b732e636f6d"
***
Lure of the Mana Stone (http://www.lotms.com/)
A Rom 2.4 Based MUD.
***
"H*","6a6a686f726e65724062656c6c736f7574682e6e6574"

Freedom is an all-or-nothing proposition:  either we 
are completely free, or we are subjects of a
tyrannical system.  If we lose one freedom in a
thousand, we become completely subjugated.

 PGP signature


Re: substitution problem

2001-05-29 Thread Me

> I am having problems using the "s///" operator for substitution in of
a
> particular pattern in a specified file.
> For eg.
>
> open(TRY,"+ while()
> {
>   $_ =~ s/a/p/g;
> }
>
> Is this the right way of doing it? And with what access mode shall I
open
> the file ( >> or +> or +< ) ? And is the while loop
> fine? What happens with this code is this code itself gets written in
> "try.txt"

Weird.

You have no print statement, so I would not
have expected any changes to be committed
back to disk, let alone somehow getting your
code!

Anyhow, the more usual perl idiom would be:

perl -pi.bak e 's/a/p/g' try.txt

or maybe:

#!/usr/bin/perl -i
while (<>)
{
s/a/p/g;
}

or other variation.




Re: substitution problem

2001-05-29 Thread Brett W. McCoy

On Tue, 29 May 2001, prachi shroff wrote:

> I am having problems using the "s///" operator for substitution in of a
> particular pattern in a specified file.
> For eg.
>
> open(TRY,"+ while()
> {
>   $_ =~ s/a/p/g;
> }

This does nothing to your file.  You need to write your data back out to a
temp file (or use the -i command-line switch) and copy the temp file to
the correct name.  There was a big thread last week on editing files in
place.  You might want to look back at the archives at
http://archive.develooper.com/beginners%40perl.org/ to see some different
options for editing files in place.

BTW, saying '$_ =~' in your code above is unnecessary because by default
the substition operator will match against $_.

-- Brett

Brett W. McCoy
Software Engineer
Broadsoft, Inc.
240-364-5225
[EMAIL PROTECTED]




Re: substitution problem

2001-05-29 Thread prachi shroff

Thanks! The method you suggested works, though it is not SIMPLE AND 
SWEET!! :-) Thanks, anyways.

Prachi


Original Message Follows
From: "J. J. Horner" <[EMAIL PROTECTED]>
To: prachi shroff <[EMAIL PROTECTED]>
CC: [EMAIL PROTECTED]
Subject: Re: substitution problem
Date: Tue, 29 May 2001 15:56:05 -0400

Is there any reason you chose not to do it this way:

# From Perl Cookbook:

open(TRY,"+< try.txt")  or die "Couldn't open try.txt: $!\n";
my @lines =# Slurp all lines into an 
array.
foreach my $line (@lines) { # Iterate through lines here.
$line =~ s/windows/linux/g; # Modify lines here.
}
seek(TRY,0,0)   or die "Error seeking file: 
$!\n";
print TRY @linesor die "Error writing to file: 
$!\n";
truncate(TRY,tell(TRY)) or die "Error truncating file: $!\n";
close(TRY)  or die "Error closing 
file: $!\n";


It may not be as short and sweet, but it works, and it is easier to debug.

JJ



* prachi shroff ([EMAIL PROTECTED]) [010529 15:41]:
 > Hi again!
 >
 > I am having problems using the "s///" operator for substitution in of a
 > particular pattern in a specified file.
 > For eg.
 >
 > open(TRY,"+ while()
 > {
 >   $_ =~ s/a/p/g;
 > }
 >
 > Is this the right way of doing it? And with what access mode shall I open
 > the file ( >> or +> or +< ) ? And is the while loop
 > fine? What happens with this code is this code itself gets written in
 > "try.txt"
 >
 >
 > Thanks,
 > Prachi
 > _
 > Get your FREE download of MSN Explorer at http://explorer.msn.com

--
J. J. Horner
"H*","6d6174686c696e40326a6e6574776f726b732e636f6d"
***
Lure of the Mana Stone (http://www.lotms.com/)
A Rom 2.4 Based MUD.
***
"H*","6a6a686f726e65724062656c6c736f7574682e6e6574"

Freedom is an all-or-nothing proposition:  either we
are completely free, or we are subjects of a
tyrannical system.  If we lose one freedom in a
thousand, we become completely subjugated.
<< attach3 >>

_
Get your FREE download of MSN Explorer at http://explorer.msn.com




Re: substitution problem

2001-05-29 Thread Me

Cookbook recipe 7.10, as quoted by JJ, has the
virtue that it does not use a temporary file.

If you will allow perl to make use of temporary files
behind the scenes (which is normally perfectly ok),
Cookbook recipe 7.9 is a whole heck of a lot
simpler. As a command line one liner:

perl -pie 's/a/p/g' try.txt foo.txt

will go thru files try.txt and foo.txt and rewrite them
with every occurence of 'a' replaced with 'p'.

Try it -- you'll like it!




Re: substitution problem

2001-05-29 Thread Jeff Pinyan

On May 29, Me said:

>If you will allow perl to make use of temporary files
>behind the scenes (which is normally perfectly ok),
>Cookbook recipe 7.9 is a whole heck of a lot
>simpler. As a command line one liner:
>
>perl -pie 's/a/p/g' try.txt foo.txt
>
>will go thru files try.txt and foo.txt and rewrite them
>with every occurence of 'a' replaced with 'p'.

>Try it -- you'll like it!

Actually, it'll say:

  Can't open perl script "s/a/p/g": No such file or directory

You need to do

  perl -pi -e 's/a/p/g' ...

The reason is because -i takes a string after it of the extension to use,
so Perl thought you wanted to use "e" as the suffix of the backup files,
and there was no -e option found.

-- 
Jeff "japhy" Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
Are you a Monk?  http://www.perlmonks.com/ http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter. Brother #734
** I need a publisher for my book "Learning Perl's Regular Expressions" **




regexp substitution problem

2002-01-14 Thread birgit kellner

use POSIX 'setlocale';
use locale;
use strict;
use warnings;
setlocale( &POSIX::LC_ALL, "de" );
my %rec;
$rec{'content'} = qq|"Madhyamaka." In: "Buddhismus in Geschichte und 
Gegenwart, Bd. I." Hamburg: Universität Hamburg (Weiterbildendes Studium) 
1998, 180-189."Zum Konzept der Leerheit im BCA." In: "Buddhismus 
in Geschichte und Gegenwart, Bd. III: ZAntidevas "Eintritt in das Leben zur 
Erleuchtung"." Hamburg: Universität Hamburg (Weiterbildendes Studium) 1999, 
61-80."The 
PrasannapadA: More Manuscripts from Nepal."  44 (2000), 
165-181."Die 
Sicht der Leerheit." Tibet und Buddhismus 52 (2000), 8-13.alkjd öslkfdj|;

if ($rec{'content'} =~ /(.*?)/s) {
print "YES: there is content with query tags\n";
my $query = "$1";
print "QUERY\n-\n$query\n-\n";
$rec{'content'} =~ s/$query/!query!/s;
print "CONTENT AFTER CHANGE: $rec{'content'}\n";
}

Does anyone know why the substitution in the second line from the bottom is 
not carried out? Does $rec{'content'} contain any illegal characters which 
allow for regexp searching, but prohibit replacing?

Thanks,

Birgit Kellner

Birgit Kellner

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: regexp substitution problem

2002-01-14 Thread Jeff 'japhy' Pinyan

On Jan 15, birgit kellner said:

>$rec{'content'} = qq|value="77">"Madhyamaka." In: "Buddhismus in Geschichte und 
>Gegenwart, Bd. I." Hamburg: Universität Hamburg (Weiterbildendes Studium) 
>1998, 180-189.value="77">"Zum Konzept der Leerheit im BCA." In: "Buddhismus 
>in Geschichte und Gegenwart, Bd. III: ZAntidevas "Eintritt in das Leben zur 
>Erleuchtung"." Hamburg: Universität Hamburg (Weiterbildendes Studium) 1999, 
>61-80."The 
>PrasannapadA: More Manuscripts from Nepal."  44 (2000), 
>165-181."Die 
>Sicht der Leerheit." Tibet und Buddhismus 52 (2000), 8-13.alkjd öslkfdj|;
>
>if ($rec{'content'} =~ /(.*?)/s) {
>   print "YES: there is content with query tags\n";
>   my $query = "$1";
>   print "QUERY\n-\n$query\n-\n";
>   $rec{'content'} =~ s/$query/!query!/s;

You want s/\Q$query\E/!query!/ instead.  $query contains some regex
metacharacters -- specifically ( and ) -- that don't match themselves, but
rather do grouping.  That is:

  "abc(def)ghi" =~ /abc(def)ghi/;

does not match, since the ( does not match a '(' character.  The \Q...\E
thing automatically escapes any potential regex character.

-- 
Jeff "japhy" Pinyan  [EMAIL PROTECTED]  http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
 what does y/// stand for?   why, yansliterate of course.


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: regexp substitution problem

2002-01-14 Thread John W. Krahn

Birgit Kellner wrote:
> 
> use POSIX 'setlocale';
> use locale;
> use strict;
> use warnings;
> setlocale( &POSIX::LC_ALL, "de" );
> my %rec;
> $rec{'content'} = qq| value="77">"Madhyamaka." In: "Buddhismus in Geschichte und
> Gegenwart, Bd. I." Hamburg: Universität Hamburg (Weiterbildendes Studium)
> 1998, 180-189. value="77">"Zum Konzept der Leerheit im BCA." In: "Buddhismus
> in Geschichte und Gegenwart, Bd. III: ZAntidevas "Eintritt in das Leben zur
> Erleuchtung"." Hamburg: Universität Hamburg (Weiterbildendes Studium) 1999,
> 61-80."The
> PrasannapadA: More Manuscripts from Nepal."  44 (2000),
> 165-181."Die
> Sicht der Leerheit." Tibet und Buddhismus 52 (2000), 8-13.alkjd öslkfdj|;
> 
> if ($rec{'content'} =~ /(.*?)/s) {
> print "YES: there is content with query tags\n";
> my $query = "$1";
> print "QUERY\n-\n$query\n-\n";
> $rec{'content'} =~ s/$query/!query!/s;
> print "CONTENT AFTER CHANGE: $rec{'content'}\n";
> }
> 
> Does anyone know why the substitution in the second line from the bottom is
> not carried out? Does $rec{'content'} contain any illegal characters which
> allow for regexp searching, but prohibit replacing?

Why not just put the substitution in the if statement?

if ( $rec{'content'} =~ s/(.*?)/!$1!/s ) {
print "YES: there is content with query tags\n";
print "QUERY\n-\n$1\n-\n";
print "CONTENT AFTER CHANGE: $rec{'content'}\n";
}



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]