Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / Perl / Getting Started / July 2008



Tip: Looking for answers? Try searching our database.

substitution

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
epanda - 30 Jun 2008 21:20 GMT
Hi,

I have to do a substitution of a pattern in a text file,

this pattern is a key of a hash table previously set.

so I want to replace my pattern by the corresponding value of the key
in the hash table

ex :

file :   n1 n22

hash :   n1 => wordA
           n2 => wordB
           n22 => wordN

I want to have filenew like this :

file :  wordA wordN

with a single %s/pattern/hashpattern/g  expression

Thanks for helping

Signature

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

Brad Baxter - 30 Jun 2008 23:27 GMT
On Jun 30, 4:20 pm, callingel...@hotmail.fr (Epanda) wrote:
> Hi,
>
> I have to do a substitution of a pattern in a text file,

So you know about perl -i, right?

> this pattern is a key of a hash table previously set.
>
[quoted text clipped - 16 lines]
>
> Thanks for helping

You mean like this?

perl -i.bak -pe '%h = (n1=>wordA, n22=>wordN); for $x (keys %h){ s/\Q
$x/$h{$x}/g }' data.txt

--
Brad

Signature

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

Amit Saxena - 01 Jul 2008 09:29 GMT
Why don't use perl "s" operator with "e" option ?

$str =~ s/([^ ]+)/$hash{\1}/ge

Regards,
Amit Saxena

> On Jun 30, 4:20 pm, callingel...@hotmail.fr (Epanda) wrote:
> > Hi,
[quoted text clipped - 36 lines]
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
John W. Krahn - 01 Jul 2008 11:49 GMT
> Why don't use perl "s" operator with "e" option ?
>
> $str =~ s/([^ ]+)/$hash{\1}/ge

You don't need the /e option to interpolate a variable in a double
quoted string and you should use $1 instead of \1 inside a double quoted
string:

$str =~ s/([^ ]+)/$hash{$1}/g

John
Signature

Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

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

Amit Saxena - 01 Jul 2008 14:01 GMT
Hi John,

I am not only expanding a variable but also using that expanded variable as
a key to ultimately find the value. That's why I need "e".

Secondly, inside text that is to be substituted, I can use \1 as well. And
moreover, for this, I don't need double quotes. If i purposefully
incorporate double quotes, then I need $1.

Regards,
Amit Saxena

>> Why don't use perl "s" operator with "e" option ?
>>
[quoted text clipped - 15 lines]
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
Rob Dixon - 01 Jul 2008 15:15 GMT
>>> Why don't use perl "s" operator with "e" option ?
>>>
[quoted text clipped - 7 lines]
> I am not only expanding a variable but also using that expanded variable as
> a key to ultimately find the value. That's why I need "e".

A single hash element is a scalar variable, and John is correct that you don't
need the /e option to interpolate it if you key it properly with $1 instead of
\1. Try it.

> Secondly, inside text that is to be substituted, I can use \1 as well. And
> moreover, for this, I don't need double quotes. If i purposefully
> incorporate double quotes, then I need $1.

John's point wasn't that double-quotes were needed, but that right-hand side of
a substitution behaves as a double-quoted string. I don't understand why you
think you need $1 if you somehow involve double quotes explicitly.

This is from

 perldoc perlre

You would do well do read and understand it so that your advice on this group
can agree with the documentation.

>   Warning on \1 vs $1
>     Some people get too used to writing things like:
[quoted text clipped - 19 lines]
>     with the operation of matching a backreference. Certainly they mean two
>     different things on the *left* side of the "s///".

In addition,

- The usual way of writing /([^ ]+)/ is /(\S+)/, and although the two are
 slightly different I doubt if you wanted your class to include tabs and
 formfeeds?

- Your code will simply delete any word in the string that doesn't appear as a
 hash key, and will also throw warnings each time it does it.

Rob

Signature

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

Rob Dixon - 01 Jul 2008 00:52 GMT
> I have to do a substitution of a pattern in a text file,
>
[quoted text clipped - 16 lines]
>
> with a single %s/pattern/hashpattern/g  expression

Something like this maybe?

HTH,

Rob

use strict;
use warnings;

my $str = 'n1 n22';

my %hash = (
 n1  => 'wordA',
 n2  => 'wordB',
 n22 => 'wordN',
);

my $re = join '|', keys %hash;
$re = qr/\b(?:$re)\b/o;

$str =~ s/($re)/$hash{$1}/g;
print $str;

Signature

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

Dr.Ruud - 02 Jul 2008 19:30 GMT
epanda schreef:

> I want to replace my pattern by the corresponding value of the key
> in the hash table [...]
> with a single %s/pattern/hashpattern/g  expression

Why that last condition? Sounds like homework.
And is unsound: often you want to do the longest possible change first.

Signature

Affijn, Ruud

"Gewoon is een tijger."

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

epanda - 03 Jul 2008 06:28 GMT
It is ok I have found the good one substitution for me but I am
amazing that perl is not as clever as vimscript regex in limitation of
assertions.

ex : perl is lost when I want to say that a digit has to not be
preceed by this sentence :
<aWord><aCharacter><aComma><aNumberOfUndefinedSpaces>
/(?<!FRUIT.;\s*)\b(\d+)
                   |_______________________the pb is here

> epanda schreef:
>
[quoted text clipped - 9 lines]
>
> "Gewoon is een tijger."

Signature

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

Dr.Ruud - 04 Jul 2008 11:20 GMT
epanda schreef:

Don't top post. Don't quote signatures and other trailers.

> rvtol:
>> epanda:

>>> I want to replace my pattern by the corresponding value of the key
>>> in the hash table [...]
[quoted text clipped - 7 lines]
> amazing that perl is not as clever as vimscript regex in limitation of
> assertions.

Define "clever". I think you just need to look much more into the
matter.

> ex : perl is lost when I want to say that a digit has to not be
> preceed by this sentence :
> <aWord><aCharacter><aComma><aNumberOfUndefinedSpaces>
> /(?<!FRUIT.;\s*)\b(\d+)
>                     |_______________________the pb is here

I don't see why you have a "\b" in there.

Should "FRUIT." match patterns like "FRUITS" and "FRUITY"?

First you say <aComma>, then you show a semicolon.

Now come up with actual example where you had a problem with.

Signature

Affijn, Ruud

"Gewoon is een tijger."

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

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.