Input

M

MJS

I need help and I have not written any code yet.

I have to ask the user to input any two digits e.g input= 3,5 (upto
300). These two numbers are associated with two different strings. As
an example, 3 is associated to "hello" and 5 is associated to "cool".
After the input, an existing file at different locations should be
updated. These locations can be determined by certain strings e.g "how
r u" and "is it hot"? After these strings, text like hello_1, hello_2
hello_3 and cool_1, cool_2 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.
...... "how r u" ..... "is it hot".......
when the user inputs 3, 5. The file should then be updated to
...... "how r u" hello_1,hello_2,hello_3 ..... "is it hot" cool_1,
cool_2, cool_3, cool_4, cool_5........

Please provide some suggestions.
 
M

Mark

Gunnar Hjalmarsson said:
Why do you need help? Because you don't want to spend the time
necessary to learn enough Perl to give it a try yourself?

Nothing wrong with that; You can always hire somebody who does the job
for you: http://jobs.perl.org/



Why do you think anybody would want to do that?

Why not? You did. ;o)
 
T

Tad McClellan

MJS said:
I need help and I have not written any code yet.

I have to ask the user to input any two digits e.g input= 3,5 (upto
300). These two numbers are associated with two different strings. As
an example, 3 is associated to "hello" and 5 is associated to "cool".


You can represent those associations in a hash:

my %word_count;
$word_count{hello} = 3;
$word_count{cool} = 5;

After the input, an existing file at different locations should be
updated. These locations can be determined by certain strings e.g "how
r u" and "is it hot"? After these strings, text like hello_1, hello_2
hello_3 and cool_1, cool_2 etc should be added to this file.


You can represent the association between a "search phrase" and
the base-word for that phrase in another hash:

my %words = ( '"how r u"' => 'hello',
'"is it hot"' => 'cool' );

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.
..... "how r u" ..... "is it hot".......
when the user inputs 3, 5. The file should then be updated to
..... "how r u" hello_1,hello_2,hello_3 ..... "is it hot" cool_1,
cool_2, cool_3, cool_4, cool_5........


----------------------------------------
#!/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}};
}

my $pattern = join '|', keys %patterns;
while (<DATA>) {
s/($pattern)/$1 $patterns{$1}/g;
print;
}


__DATA__
...... "how r u" ..... "is it hot".......
 

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,129
Messages
2,570,770
Members
47,329
Latest member
FidelRauch

Latest Threads

Top