Appending at various locations.

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);
======================================
 
J

James E Keenan

MJS said:
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.
[script snipped]

.... And your question is?
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

(e-mail address removed) (MJS) wrote in
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
....

Didn't you post this about a month ago?

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP1xZ0GPeouIeTNHoEQJZvgCg8Y60jVSZeUxbTGt3NxDgAy4RX18AoKGl
gKqzdoSxqxYrn85FjrjCRVPU
=y0Ca
-----END PGP SIGNATURE-----
 
T

Tad McClellan

Eric J. Roode said:
(e-mail address removed) (MJS) wrote in

...

Didn't you post this about a month ago?


He posted the question 2 weeks ago.

I posted (most of) the code 2 weeks ago in response.

It appears that he needs to modify what he was given, but he
neglected to mention what problem it is that he needs solved...


It looks to me like he wants us to write his program for him rather
than help him learn how to write his program.
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top