M
MJS
I have to ask the user to input any two digits e.g input= 3,5 (their
value can be upto 300-whole numbers only). These two numbers have to
be associated with multiple strings. As an example, 3 is associated
to "hello" and 5 is associated to "cool". After the input, an existing
file at various locations should be edited automatically. These
locations can be determined by certain strings e.g "howr u" and "is
it hot" etc? Once these strings are searched and found, then text like
hello_1, hello_2, hello_3 and cool_1, cool_2,cool_3,cool_4, etc
should be added to this file. The
number of times hello_1, hello_2 etc and cool_1, cool_2 etc. should
be added depends on the users input.
e.g
a file contains some text.
some text..... i m from the city..... "how r u" ..... how is the weather there
"is it hot".......thats good.
when the user inputs 3, 5. The file should then be edited to
some text ..... i m from the city ...."how r u" hello_1,hello_2,hello_3 .....
how is the weather there "is it hot" cool_1,cool_2, cool_3, cool_4,
cool_5........thats good.
============================================
#!/usr/bin/perl
use strict;
use warnings;
print "enter counts for 'hello' and 'cool' with a space in
between: ";
my $nums = <STDIN>;
# key=word, value=count for that word
my %word_count;
@word_count{ 'hello', 'cool' } = split /\s+/, $nums; # a "hash
slice"
# key=search phrase, value=the word to tack on when found
my %words = ( '"how r u"' => 'hello', '"is it hot"' => 'cool' );
# key=search phrase, value=string with the right # of words/commas
my %patterns;
foreach my $pat ( keys %words ) {
$patterns{$pat} = join ',', map { "$words{$pat}_$_" }
1 .. $word_count{$words{$pat}};
}
open(FILEREAD, "data") or die "Can't open data: $!\n";
my $pattern = join '|', keys %patterns;
open(F,">>data") or die "Can't open data: $!\n";
while (<FILEREAD>) {
s/($pattern)/$1 $patterns{$1}/g;
print F $_;
};
close(F);
close(FILEREAD);
======================================
value can be upto 300-whole numbers only). These two numbers have to
be associated with multiple strings. As an example, 3 is associated
to "hello" and 5 is associated to "cool". After the input, an existing
file at various locations should be edited automatically. These
locations can be determined by certain strings e.g "howr u" and "is
it hot" etc? Once these strings are searched and found, then text like
hello_1, hello_2, hello_3 and cool_1, cool_2,cool_3,cool_4, etc
should be added to this file. The
number of times hello_1, hello_2 etc and cool_1, cool_2 etc. should
be added depends on the users input.
e.g
a file contains some text.
some text..... i m from the city..... "how r u" ..... how is the weather there
"is it hot".......thats good.
when the user inputs 3, 5. The file should then be edited to
some text ..... i m from the city ...."how r u" hello_1,hello_2,hello_3 .....
how is the weather there "is it hot" cool_1,cool_2, cool_3, cool_4,
cool_5........thats good.
============================================
#!/usr/bin/perl
use strict;
use warnings;
print "enter counts for 'hello' and 'cool' with a space in
between: ";
my $nums = <STDIN>;
# key=word, value=count for that word
my %word_count;
@word_count{ 'hello', 'cool' } = split /\s+/, $nums; # a "hash
slice"
# key=search phrase, value=the word to tack on when found
my %words = ( '"how r u"' => 'hello', '"is it hot"' => 'cool' );
# key=search phrase, value=string with the right # of words/commas
my %patterns;
foreach my $pat ( keys %words ) {
$patterns{$pat} = join ',', map { "$words{$pat}_$_" }
1 .. $word_count{$words{$pat}};
}
open(FILEREAD, "data") or die "Can't open data: $!\n";
my $pattern = join '|', keys %patterns;
open(F,">>data") or die "Can't open data: $!\n";
while (<FILEREAD>) {
s/($pattern)/$1 $patterns{$1}/g;
print F $_;
};
close(F);
close(FILEREAD);
======================================