word replacement

T

t.poisot

Hi there

I'm currently working on a little script wich will look for acronyms in
a file, and then replace them by the html acronym tag.

I have a text file to read, and two tables ( @mot and @remplacement )
containing respectively the words and the html code.

I use the following code :

sub parse_ligne
{
my $ligne = $_;
for ( my $i = 0 ; $i < $dictionnaire_taille-1 ; $i++ )
{
if ($ligne =~ m/$mot[$i]/gi)
{
$ligne =~ s/$mot[$i]/$remplacement[$i]/gi;
}
}
//print $ligne;
print `echo $ligne >> $path_sortie`;

}

But
- Acronyms are not replaced
- Only the last line of the file is printed out in the output file

Does anybody have any idea on how to get rid of that ?

thx
 
D

Dr.Ruud

(e-mail address removed) schreef:


fup set to clpm
I'm currently working on a little script wich will look for acronyms
in a file, and then replace them by the html acronym tag.

I have a text file to read, and two tables ( @mot and @remplacement )
containing respectively the words and the html code.

I use the following code :

sub parse_ligne
{
my $ligne = $_;
for ( my $i = 0 ; $i < $dictionnaire_taille-1 ; $i++ )
{
if ($ligne =~ m/$mot[$i]/gi)
{
$ligne =~ s/$mot[$i]/$remplacement[$i]/gi;
}
}
//print $ligne;
print `echo $ligne >> $path_sortie`;

}

But
- Acronyms are not replaced
- Only the last line of the file is printed out in the output file

Does anybody have any idea on how to get rid of that ?

That is not code that we can run. Please read the Posting Guidelines.
Cross-posting to clp-misc and clp-moderated, is a bad idea.


You can run this code on itself:

=================
#!/usr/bin/perl
# Search & Replace demo
use strict ;
use warnings ;

my @sr ;
while ( <DATA> ) {
chomp ;
push @sr, [ split /,[[:blank:]]*/ ] ;
}

while ( my $line = <> ) {
$line =~ s/\b\Q$$_[0]\E\b/$$_[1]/gi for @sr ;
print $line ;
}

__DATA__
initial, primary
1st, first
2nd, second
3rd, third
first, initial
=================

Save it as sr-demo.pl and run
perl sr-demo.pl < sr-demo.pl
 
L

Le_T

Dr.Ruud said:
(e-mail address removed) schreef:


fup set to clpm
I'm currently working on a little script wich will look for acronyms
in a file, and then replace them by the html acronym tag.

I have a text file to read, and two tables ( @mot and @remplacement )
containing respectively the words and the html code.

I use the following code :

sub parse_ligne
{
my $ligne = $_;
for ( my $i = 0 ; $i < $dictionnaire_taille-1 ; $i++ )
{
if ($ligne =~ m/$mot[$i]/gi)
{
$ligne =~ s/$mot[$i]/$remplacement[$i]/gi;
}
}
//print $ligne;
print `echo $ligne >> $path_sortie`;

}

But
- Acronyms are not replaced
- Only the last line of the file is printed out in the output file

Does anybody have any idea on how to get rid of that ?

That is not code that we can run. Please read the Posting Guidelines.
Cross-posting to clp-misc and clp-moderated, is a bad idea.


You can run this code on itself:

=================
#!/usr/bin/perl
# Search & Replace demo
use strict ;
use warnings ;

my @sr ;
while ( <DATA> ) {
chomp ;
push @sr, [ split /,[[:blank:]]*/ ] ;
}

while ( my $line = <> ) {
$line =~ s/\b\Q$$_[0]\E\b/$$_[1]/gi for @sr ;
print $line ;
}

__DATA__
initial, primary
1st, first
2nd, second
3rd, third
first, initial
=================

Save it as sr-demo.pl and run
perl sr-demo.pl < sr-demo.pl

I'm sorry, am not very aware of usenet practices... ;s

I think the problem comes form my reg exp (and to be perfectly honnest,
I'd prefer solve the problem by myself, not just copy and paste another
code / that i won't be able to understand/ from a perl guru...)
However, I thank you a lot

If that is my regexp
:
$ligne =~ s/$mot[$i]/$remplacement[$i]/gi;

what is the problem ? Is it not uspposed to replace $mot[$i] by
$remplacement[$i] ?

is tehre any options i need to add at the end ?
 
T

Tad McClellan

[snip full-quote, please don't do that, trim the parts that
are not needed to establish context.
]



[snip short and complete program that the _OP_ can run.]



Please do not quote .sigs.

I'm sorry, am not very aware of usenet practices... ;s


We can tell.

You should fix that as soon as possible.

(and to be perfectly honnest,
I'd prefer solve the problem by myself, not just copy and paste another
code / that i won't be able to understand/ from a perl guru...)


That is a very good approach to learning.

Another is to question the guru until you _do_ understand the code. :)

If that is my regexp
:
$ligne =~ s/$mot[$i]/$remplacement[$i]/gi;

what is the problem ?


To answer that, we need to know the values of 4 variables:

$ligne
$i
$mot[$i]
$remplacement[$i]

But we only know the value of 1 of the 4, so all we can do is guess
(or forget about trying to answer altogether).

If you post a short and complete program *that we can run*, then
someone will surely be able to help you fix your problem.

Is it not uspposed to replace $mot[$i] by
$remplacement[$i] ?


Yes.

Unless $mot[$i] contains regex metacharacters that you want
to match literally.

Or unless $ligne does not match $mot[$i].

is tehre any options i need to add at the end ?


I dunno, but I am quite sure that there is a option that
you want to *remove* from the end.

If

$ligne = 'Ask us.';
$mot[$i] = 'US';
$remplacement[$i] = 'United States';

then $ligne will become 'Ask United States.'

So, you probably don't want the s///i option.
 
D

Dr.Ruud

Le_T schreef:
I'm sorry, am not very aware of usenet practices... ;s

The Posting Guidelines for this group are regularly posted in this
group.

I think the problem comes form my reg exp
$ligne =~ s/$mot[$i]/$remplacement[$i]/gi;

what is the problem ? Is it not uspposed to replace $mot[$i] by
$remplacement[$i] ?

You showed a strange looking function pars_ligne() that was not used in
your code.
First post the minimal, but complete, code that doesn't do what you
expect it to, so that we can check it.
Alternative: ask questions about the code in my version.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,263
Messages
2,571,064
Members
48,769
Latest member
Clifft

Latest Threads

Top