CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Substitutions

 



IsabelleFr
Novice

Feb 12, 2013, 8:05 PM

Post #1 of 9 (697 views)
Substitutions Can't Post

Hello! I'm new to Perl and i'm trying to figure out a way to substitute some predefined words in a text. For exemple: "John has three apples, two cats and one girlfriend." I'd like to substitute the bold words for their decimal representation (3, 2, 1 in the exemple above) and be able to make that substitution happen whenever one of these predefined words is found in the input text. I heard that a hash can be a good way to do it but i just can't do it. Thanks for helping me!


rovf
Veteran

Feb 12, 2013, 11:55 PM

Post #2 of 9 (691 views)
Re: [PerlNab] Substitutions [In reply to] Can't Post

1. At the very beginning of your program, write

use strict;
use warnings FATAL => 'all';

2. Next define a hash which maps the keywords you are looking for, into numbers.

3. Split your input string into an array of single words.

4. Loop through this array, and if a word exists as key in your hash, replay it by the hash value.

5. Put the words of the array to a string together again.

That's it!


IsabelleFr
Novice

Feb 13, 2013, 6:11 AM

Post #3 of 9 (683 views)
Re: [rovf] Substitutions [In reply to] Can't Post

Hey, ty! I've tried something but it's not yet what i wanted cause i can't do the replacement and recover all the string with the replaced items:

use strict;
use warnings;

my $ligne = <STDIN>;
my %norm = ("one" => 1, "two" => 2, "three" => 3);
my @array = split(' ', $ligne);

foreach my $elem (@array) {
if (exists $norm{$elem}) {
print "$norm{$elem}\n";
}
}

This will only print the value of the key if it is also in the array :X
I woud like to be able to write down "bobby has two cats and one dog" and get "bobby has 2 cats and 1 dog".


(This post was edited by IsabelleFr on Feb 13, 2013, 6:13 AM)


rovf
Veteran

Feb 13, 2013, 6:46 AM

Post #4 of 9 (678 views)
Re: [IsabelleFr] Substitutions [In reply to] Can't Post


Quote
This will only print the value of the key if it is also in the array


Well, this is what you told the program to do.

So if it is not in the array, you just have to print the word.

Please have a look at perldoc perlsyn. It describes the else clause to the if statement.


rovf
Veteran

Feb 13, 2013, 6:55 AM

Post #5 of 9 (676 views)
Re: [IsabelleFr] Substitutions [In reply to] Can't Post

One more thing: Don't forget to test your program also for this input:

Yesterday, we were two, but today we are three


pdoherty
New User

Feb 13, 2013, 8:14 AM

Post #6 of 9 (670 views)
Re: [IsabelleFr] Substitutions [In reply to] Can't Post

Why not just do something like this:


Code
while (<IN>) {  #read file of lines of data 
$_ =~ s/\bthree\b/3/gi;
$_ =~ s/\btwo\b/2/gi;
$_ =~ s/\bone\b/1/gi;
print OUT $_; # assuming we are creating a new file with the modified lines
}



BillKSmith
Veteran

Feb 13, 2013, 10:04 AM

Post #7 of 9 (661 views)
Re: [rovf] Substitutions [In reply to] Can't Post

It is not necessary to split the text. Use the /e option to evaluate the hash in the replacement field of the substitution.

Code
use strict; 
use warnings;
my %number = (
one => 1,
two => 2,
three => 3,
);

while (my $line = <DATA>) {
$line =~ s/(:?one|two|three)/$number{lc $1}/egi;
print $line, "\n";
}
__DATA__
John has three apples, two cats and one girlfriend

Ouput:

Code
John has 3 apples, 2 cats and 1 girlfriend


EDIT: Minor correction to make code case insensitive.
Good Luck,
Bill

(This post was edited by BillKSmith on Feb 13, 2013, 10:18 AM)


IsabelleFr
Novice

Feb 13, 2013, 5:32 PM

Post #8 of 9 (650 views)
Re: [pdoherty] Substitutions [In reply to] Can't Post

As i have to cover a large list of numbers i prefer something more economic than listing all cases to substitute ;)


IsabelleFr
Novice

Feb 13, 2013, 5:35 PM

Post #9 of 9 (648 views)
Re: [BillKSmith] Substitutions [In reply to] Can't Post

Wow! This is great! Thanks a lot <3

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives